我有一个 JSF 应用程序在 GlassFish 3.1.2.2 上运行,带有 Mojarra 2.1.21 和 OmniFaces 1.4.1 来处理 Ajax 错误。该应用程序具有受保护区域和公共区域,使用基于表单的身份验证(在 JSF 表单上)和编程登录。
当用户单击受保护页面上的命令按钮时,我总是会得到一个空白页面,该页面由底层ServletException:意外错误转发或重定向到登录页面引起。
如果我错了,请纠正我,但从日志来看,这似乎是由于 Web 容器转发到 login.xhtml 但仍在尝试恢复旧视图 - 最终以 ViewExpiredException 包装成 ServletException,所以没有错误-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>
- 添加 servlet 过滤器以捕获 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; } } }
- 使
login.xhtml
无状态(通过<f:view transient = "true">
)没有帮助,因为 Faces Servlet 试图恢复的是过期页面的视图。
从 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>