1

Ajax エラーを処理するために Mojarra 2.1.21 および OmniFaces 1.4.1 を使用して GlassFish 3.1.2.2 で JSF アプリを実行しています。アプリには、プログラムによるログインでフォームベースの認証 (JSF フォーム上) を使用して、保護領域と公開領域の両方があります。

基本的なServletException : Unexpected error forwarding or redirecting to login page .

私が間違っている場合は修正してください。ただし、ログからは、web コンテナーが login.xhtml に転送されているにもかかわらず、古いビューを復元しようとしていることが原因のようです。 - web.xml で定義されたページを照合して表示できました。例外は次のとおりです。

    [#|2013-05-07T16:51:12.175+0800|WARNING|glassfish3.1.2|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=105;_ThreadName=Thread-2;|ApplicationDispatcher[] PWC1231: Servlet.service() for servlet Faces Servlet threw exception
javax.faces.application.ViewExpiredException: viewId:/login.xhtml - View /login.xhtml could not be restored.
    at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:205)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:116)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
    at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:809)
    ...
|#]

[#|2013-05-07T16:51:12.176+0800|WARNING|glassfish3.1.2|org.apache.catalina.authenticator.FormAuthenticator|_ThreadID=105;_ThreadName=Thread-2;|Unexpected error forwarding or redirecting to login page
javax.servlet.ServletException: viewId:/login.xhtml - View /login.xhtml could not be restored.
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
    at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:809)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:671)
    ...
Caused by: javax.faces.application.ViewExpiredException: viewId:/login.xhtml - View /login.xhtml could not be restored.
    at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:205)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:116)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    ... 32 more
|#]

私は何時間もそれをキャッチしようとしましたが、いくつかの方法で無駄になりました:

  • ログインページの構成に追加faces-redirect=trueしても、結果ではないため効果はありません
 <form-login-config>
        <form-login-page>/login.xhtml?faces-redirect=true</form-login-page>
        <form-error-page>/login.xhtml?faces-redirect=true</form-error-page>
    </form-login-config>
  • 別の投稿で述べたように、コンテナー管理のセキュリティがフィルターの前に作動するため、rootCause をキャッチするためのサーブレット フィルターの追加は機能しませんでした。
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
    try {
        chain.doFilter(request, response);
    } catch (ServletException e) {
        Throwable rootCause = e.getRootCause();
        if (rootCause instanceof ViewExpiredException) { // This is true for any FacesException.
            throw (ViewExpiredException) rootCause; // Throw wrapped ViewExpiredException instead of ServletException.
        } else if (rootCause instanceof RuntimeException) { // This is true for any FacesException.
            throw (RuntimeException) rootCause; // Throw wrapped RuntimeException instead of ServletException.
        } else {
            throw e;
        }
    }
}
  • Faces Servlet が復元しようとしていたのは期限切れのページのビューだったため、login.xhtmlステートレス<f:view transient = "true">にすることは役に立ちませんでした。

ここ StackOverflow に関する多数の投稿から、いくつかのオプションがあることがわかります。

  • ログインページを非 JSF ページにしますが、ログインページに動的データが必要です
  • カスタム エラー ハンドラーを使用します。これは機能しましたが、OmniFaces の FullAjaxException ハンドラーをオーバーライドし、web.xml のエラー ページ宣言を尊重しなくなりました。

私が知りたいのは、この例外を適切に処理する他の方法はありますか? ログインページへの転送を許可するか、エラーページを表示します。(FullAjaxExceptionHandler に相当する非 Ajax は完璧です!)

よろしくお願いします。

私のweb.xml:

> <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
        <param-value>true</param-value>
    </context-param>
    <!-- See annotation declared servlets, converters, and filters in faces and servlet folders -->
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            1
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
    <error-page>
        <exception-type>javax.faces.application.ViewExpiredException</exception-type>
        <location>/WEB-INF/errorpages/expired.xhtml</location>
    </error-page>
    <error-page>
        <exception-type>java.lang.RuntimeException</exception-type>
        <location>/WEB-INF/errorpages/error.xhtml</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/WEB-INF/errorpages/error.xhtml</location>
    </error-page>
    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/errorpages/404.xhtml</location>
    </error-page>
    <!-- SECURITY -->
    <security-constraint>
        <display-name>AdminPagesConstraint</display-name>
        <web-resource-collection>
            <web-resource-name>AdminResource</web-resource-name>
            <description/>
            <url-pattern>/admin/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <description/>
            <role-name>ADMIN</role-name>
        </auth-constraint>
    </security-constraint>
    <security-constraint>
        <display-name>CustomerPagesConstraint</display-name>
        <web-resource-collection>
            <web-resource-name>CustomerResource</web-resource-name>
            <description/>
            <url-pattern>/customer/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <description/>
            <role-name>CUSTOMER</role-name>
        </auth-constraint>
    </security-constraint>
    <login-config>
        <auth-method>FORM</auth-method>
        <realm-name>App_Realm</realm-name>
        <form-login-config>
            <form-login-page>/login.xhtml</form-login-page>
            <form-error-page>/login.xhtml</form-error-page><!-- no use for programmatic login -->
        </form-login-config>
    </login-config>
    <security-role>
        <description/>
        <role-name>CUSTOMER</role-name>
    </security-role>
    <security-role>
        <description/>
        <role-name>ADMIN</role-name>
    </security-role>
</web-app>
4

1 に答える 1

0

以下のスニペットを web.xml に含めます。それは私のために働いた

<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/login.xhtml?faces-redirect=true</location>
</error-page>

でもコメントアウト

<error-page>
    <error-code>500</error-code>
    <location>/WEB-INF/errorpages/error.xhtml</location>
</error-page>
于 2013-05-25T18:10:30.360 に答える