这个问题与此问题非常相似:Using JSF 2.0 / Facelets, is there a way to attach a global listener to all AJAX calls? 但解决方案对我不起作用。我错过了什么。
问题是:
会话过期后,我点击一些ajax链接/按钮并发送重定向(来自Filter
)浏览器不更新页面 接收最后一个非AJAX请求的页面< / em>的
我拥有的是:
@ManagedBean(name = "user") @SessionScoped UserBean
与字段User user
一起使用(后者又是用于用户访问管理的EJB实体类)FullAjaxExceptionHandler
(由于BalusC,它没有我的过滤器就可以正常工作)我使用javax.servlet.Filter
maped到@WebFilter("*.xhtml")
来管理资源访问和登录检查。 doFilter()
功能代码段:
....
if (user == null) { // if user is not logged in
redirectToLogin(req, res);
}
在同一个Filter类中有redirectToLogin()
函数(再次感谢BalusC(=):
private static final String FACES_REDIRECT_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<partial-response><redirect url=\"%s\"></redirect></partial-response>";
protected void redirectToLogin(ServletRequest req, ServletResponse res) throws IOException {
HttpServletRequest request = (HttpServletRequest) req;
if ("partial/ajax".equals(request.getHeader("Faces-Request"))) {
res.setContentType("text/xml");
res.setCharacterEncoding("UTF-8");
res.getWriter().printf(FACES_REDIRECT_XML, request.getContextPath() + "/login.xhtml");
} else {
String url = request.getContextPath() + "/login.xhtml";
HttpServletResponse resHttp = (HttpServletResponse) res;
resHttp.sendRedirect(url);
}
}
当会话过期时,我点击一些ajax链接/按钮(发生ViewExpiredException)。我可以在调试中看到字符串res.getWriter().printf(FACES_REDIRECT_XML, request.getContextPath() + "/login.xhtml");
执行。我还可以通过我的 login.xhtml (,这不是登录页面,也就是最后一个非AJAX请求的旧页面,看到HTTP REDIRECT请求和响应!)Firebug中的页面,但浏览器不更新视图。
我可以以某种方式检测到在Filter(FacesContext外)中发生ViewExpiredException并将控制传递给FullAjaxExceptionHandler吗? (顺便说一句,如果有可能那么我不再需要ajaxExceptionHandler用于ViewExpiredException,对吧?) 浏览器页面没有更新的原因是什么?
正如我在帖子顶部提到的那样,我阅读并遵循相关post指南,但我错过了一些或误解了。它可能是什么?
感谢。
更新:我收到的HTML代码是最后一次非AJAX请求返回的旧HTML页面!因此,FacesServlet不会将我的login.xhtml发送到客户端的浏览器。
Update2:我发现从FasesServlet收到的页面是最后一个非AJAX请求页面(在我的例子中,它是我成功登录后看到的第一个视图)。 (顺便说一句,浏览器仍然没有更新页面,并且存在由此行引起的XML解析错误:<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
)
Update3: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.PARTIAL_STATE_SAVING</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>facelets.DEVELOPMENT</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<session-config>
<session-timeout>
1
</session-timeout>
</session-config>
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
<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>
<welcome-file-list>
<welcome-file>login.xhtml</welcome-file>
</welcome-file-list>
<context-param>
<param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
<param-value>true</param-value>
</context-param>
<error-page>
<error-code>500</error-code>
<location>/errors/500.xhtml</location>
</error-page>
<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/loginViewExpired.xhtml</location>
</error-page>
</web-app>