JSF2中AJAX请求中ViewExpiredException的重定向
当我的会话在JavaEE7、JSFWeb应用程序中过期时 我在ajax请求中得到一个ViewExpiredException 我想重定向到一个页面,它向用户显示会话已过期 我试着浏览谷歌和stackoverflow寻找解决方案,但我没有任何运气让它按照我想要的方式工作 更新: 我尝试了发布的解决方案@ 当使用login_submit按钮时,它确实在我的登录页面工作JSF2中AJAX请求中ViewExpiredException的重定向,ajax,jsf-2,primefaces,Ajax,Jsf 2,Primefaces,当我的会话在JavaEE7、JSFWeb应用程序中过期时 我在ajax请求中得到一个ViewExpiredException 我想重定向到一个页面,它向用户显示会话已过期 我试着浏览谷歌和stackoverflow寻找解决方案,但我没有任何运气让它按照我想要的方式工作 更新: 我尝试了发布的解决方案@ 当使用login_submit按钮时,它确实在我的登录页面工作 <ui:composition xmlns="http://www.w3.org/1999/xhtml" xm
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://xmlns.jcp.org/jsf/html"
template="./../templates/login_template.xhtml">
<ui:define name="title">
<h:outputText value="#{bundle.login_title}"/>
</ui:define>
<ui:define name="content">
<h:outputText escape="false" value="#{bundle.login_message}"/>
<h:form>
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel for="username" value="#{bundle.login_username}"/>
<p:inputText id="username" type="text" value="#{authBean.username}" label="username"/>
<h:outputLabel for="password" value="#{bundle.login_password}"/>
<p:inputText id="password" type="password" value="#{authBean.password}" label="password"/>
<h:outputText value="#{bundle.login_invalid_password}" rendered="#{!authBean.validPassword and authBean.validUsername}"/>
<h:outputText value="#{bundle.login_invalid_username}" rendered="#{!authBean.validUsername}"/>
<p:commandButton value="#{bundle.login_submit}" action="#{authBean.doLogin}"/>
</h:panelGrid>
</h:form>
</ui:define>
</ui:composition>
有人知道如何解决这个问题吗。我在webfilter中这样做,效果很好:
// Check if user logged in, if not redirect to login.xhtml
if (loginBean == null || !((LoginBean) loginBean).isLoggedIn()) {
boolean isAjax = "XMLHttpRequest".equals(req.getHeader("X-Requested-With"));
if (!isAjax) {
res.sendRedirect(req.getContextPath() + "/login.xhtml");
} else {
// Redirecting an ajax request has to be done in the following way:
// http://javaevangelist.blogspot.com/2013/01/jsf-2x-tip-of-day-ajax-redirection-from.html
String redirectURL = res.encodeRedirectURL(req.getContextPath() + "/login.xhtml");
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><partial-response><redirect url=\"").append(redirectURL).append("\"></redirect></partial-response>");
res.setCharacterEncoding("UTF-8");
res.setContentType("text/xml");
PrintWriter pw = response.getWriter();
pw.println(sb.toString());
pw.flush();
}
} else {
// Let chain of filters continue;
chain.doFilter(request, response);
}
//检查用户是否登录,如果没有,则重定向到login.xhtml
if(loginBean==null | |!((loginBean)loginBean.isLoggedIn()){
布尔值isAjax=“XMLHttpRequest.”等于(req.getHeader(“X-request-With”);
if(!isAjax){
res.sendRedirect(req.getContextPath()+“/login.xhtml”);
}否则{
//必须按照以下方式重定向ajax请求:
// http://javaevangelist.blogspot.com/2013/01/jsf-2x-tip-of-day-ajax-redirection-from.html
String redirectURL=res.encodeRedirectURL(req.getContextPath()+“/login.xhtml”);
StringBuilder sb=新的StringBuilder();
某人加上(“”);
res.setCharacterEncoding(“UTF-8”);
res.setContentType(“text/xml”);
PrintWriter pw=response.getWriter();
println(sb.toString());
pw.flush();
}
}否则{
//让过滤器链继续运行;
链式过滤器(请求、响应);
}
我在webfilter中这样做,效果很好:
// Check if user logged in, if not redirect to login.xhtml
if (loginBean == null || !((LoginBean) loginBean).isLoggedIn()) {
boolean isAjax = "XMLHttpRequest".equals(req.getHeader("X-Requested-With"));
if (!isAjax) {
res.sendRedirect(req.getContextPath() + "/login.xhtml");
} else {
// Redirecting an ajax request has to be done in the following way:
// http://javaevangelist.blogspot.com/2013/01/jsf-2x-tip-of-day-ajax-redirection-from.html
String redirectURL = res.encodeRedirectURL(req.getContextPath() + "/login.xhtml");
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><partial-response><redirect url=\"").append(redirectURL).append("\"></redirect></partial-response>");
res.setCharacterEncoding("UTF-8");
res.setContentType("text/xml");
PrintWriter pw = response.getWriter();
pw.println(sb.toString());
pw.flush();
}
} else {
// Let chain of filters continue;
chain.doFilter(request, response);
}
//检查用户是否登录,如果没有,则重定向到login.xhtml
if(loginBean==null | |!((loginBean)loginBean.isLoggedIn()){
布尔值isAjax=“XMLHttpRequest.”等于(req.getHeader(“X-request-With”);
if(!isAjax){
res.sendRedirect(req.getContextPath()+“/login.xhtml”);
}否则{
//必须按照以下方式重定向ajax请求:
// http://javaevangelist.blogspot.com/2013/01/jsf-2x-tip-of-day-ajax-redirection-from.html
String redirectURL=res.encodeRedirectURL(req.getContextPath()+“/login.xhtml”);
StringBuilder sb=新的StringBuilder();
某人加上(“”);
res.setCharacterEncoding(“UTF-8”);
res.setContentType(“text/xml”);
PrintWriter pw=response.getWriter();
println(sb.toString());
pw.flush();
}
}否则{
//让过滤器链继续运行;
链式过滤器(请求、响应);
}
我在webfilter中这样做,效果很好:
// Check if user logged in, if not redirect to login.xhtml
if (loginBean == null || !((LoginBean) loginBean).isLoggedIn()) {
boolean isAjax = "XMLHttpRequest".equals(req.getHeader("X-Requested-With"));
if (!isAjax) {
res.sendRedirect(req.getContextPath() + "/login.xhtml");
} else {
// Redirecting an ajax request has to be done in the following way:
// http://javaevangelist.blogspot.com/2013/01/jsf-2x-tip-of-day-ajax-redirection-from.html
String redirectURL = res.encodeRedirectURL(req.getContextPath() + "/login.xhtml");
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><partial-response><redirect url=\"").append(redirectURL).append("\"></redirect></partial-response>");
res.setCharacterEncoding("UTF-8");
res.setContentType("text/xml");
PrintWriter pw = response.getWriter();
pw.println(sb.toString());
pw.flush();
}
} else {
// Let chain of filters continue;
chain.doFilter(request, response);
}
//检查用户是否登录,如果没有,则重定向到login.xhtml
if(loginBean==null | |!((loginBean)loginBean.isLoggedIn()){
布尔值isAjax=“XMLHttpRequest.”等于(req.getHeader(“X-request-With”);
if(!isAjax){
res.sendRedirect(req.getContextPath()+“/login.xhtml”);
}否则{
//必须按照以下方式重定向ajax请求:
// http://javaevangelist.blogspot.com/2013/01/jsf-2x-tip-of-day-ajax-redirection-from.html
String redirectURL=res.encodeRedirectURL(req.getContextPath()+“/login.xhtml”);
StringBuilder sb=新的StringBuilder();
某人加上(“”);
res.setCharacterEncoding(“UTF-8”);
res.setContentType(“text/xml”);
PrintWriter pw=response.getWriter();
println(sb.toString());
pw.flush();
}
}否则{
//让过滤器链继续运行;
链式过滤器(请求、响应);
}
我在webfilter中这样做,效果很好:
// Check if user logged in, if not redirect to login.xhtml
if (loginBean == null || !((LoginBean) loginBean).isLoggedIn()) {
boolean isAjax = "XMLHttpRequest".equals(req.getHeader("X-Requested-With"));
if (!isAjax) {
res.sendRedirect(req.getContextPath() + "/login.xhtml");
} else {
// Redirecting an ajax request has to be done in the following way:
// http://javaevangelist.blogspot.com/2013/01/jsf-2x-tip-of-day-ajax-redirection-from.html
String redirectURL = res.encodeRedirectURL(req.getContextPath() + "/login.xhtml");
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><partial-response><redirect url=\"").append(redirectURL).append("\"></redirect></partial-response>");
res.setCharacterEncoding("UTF-8");
res.setContentType("text/xml");
PrintWriter pw = response.getWriter();
pw.println(sb.toString());
pw.flush();
}
} else {
// Let chain of filters continue;
chain.doFilter(request, response);
}
//检查用户是否登录,如果没有,则重定向到login.xhtml
if(loginBean==null | |!((loginBean)loginBean.isLoggedIn()){
布尔值isAjax=“XMLHttpRequest.”等于(req.getHeader(“X-request-With”);
if(!isAjax){
res.sendRedirect(req.getContextPath()+“/login.xhtml”);
}否则{
//必须按照以下方式重定向ajax请求:
// http://javaevangelist.blogspot.com/2013/01/jsf-2x-tip-of-day-ajax-redirection-from.html
String redirectURL=res.encodeRedirectURL(req.getContextPath()+“/login.xhtml”);
StringBuilder sb=新的StringBuilder();
某人加上(“”);
res.setCharacterEncoding(“UTF-8”);
res.setContentType(“text/xml”);
PrintWriter pw=response.getWriter();
println(sb.toString());
pw.flush();
}
}否则{
//让过滤器链继续运行;
链式过滤器(请求、响应);
}
@Wesley Egbertsen抱歉,那不是我的真实代码,现在已经发布了。。。你能检查一下对你是否也合适吗?如果我没记错的话,我在检测之前的代码是否是ajax请求时遇到了问题我已经有了自己的功能来检测它是否是ajax请求,我真的很接近解决方案,但是代码中返回部分响应并重定向的部分救了我!下面是我检查它是否是AJAX请求的方法:
private boolean isAJAXRequest(ServletRequest){HttpServletRequest req=(HttpServletRequest)request;return req.getHeader(“faces request”)!=null&&req.getHeader(“faces request”).toLowerCase().indexOf(“AJAX”)>-1;
@Wesley Egbertsen抱歉,这不是我真正的代码,现在已经发布了。。。你能检查一下对你是否也合适吗?如果我没记错的话,我在检测之前的代码是否是ajax请求时遇到了问题。我已经有了自己的功能来检测它是否是ajax请求,我真的很接近一个解决方案,但是