2015-10-15 88 views
0

當我的會話在我的Java EE 7,JSF Web應用程序中過期時。在JSF 2中的AJAX請求中的ViewExpiredException上的重定向

我在ajax請求中得到ViewExpiredException。

我想重定向到一個頁面,它向用戶顯示會話已過期。

我試過瀏覽谷歌和stackoverflow的解決方案,但我did not沒有任何運氣,讓它與我想要它的工作。

UPDATE:

我嘗試使用login_submit按鈕時,該解決方案張貼@Session timeout and ViewExpiredException handling on JSF/PrimeFaces ajax request

它沒有工作,在我的登錄頁面。

<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> 

不過,這並不與此JSF的XHTML頁面是安全網頁上工作,登錄頁面是一個公共頁面。例如,下面的XHTML是安全網頁上:

<p:commandButton styleClass="button #{chartBean.isViewButtonActive('MONTH')}" update="dataChart dataTable @form" value="#{bundle.performance_year}" action="#{chartBean.changeModel('MONTH')}" /> 

這確實一個AJAX POST請求,但被我的@WebFilter逮住。 (它也發生在Primefaces中的selectOneMenu)這個過濾器檢查用戶是否登錄,如果沒有登錄,它將它們重定向到登錄頁面。但出於某種原因,使用上面給出的示例按鈕,它也會被@WebFilter捕獲,並且ajax請求將作爲@WebFilter中指定的響應重定向獲取。它不會被ExceptionHandler捕獲。 @WebFilter僅適用於安全頁面,請參見下文。

@WebFilter(
     filterName = "AuthorizationFilter", 
     urlPatterns = {"/secure/*"}, 
     dispatcherTypes = {DispatcherType.REQUEST, DispatcherType.FORWARD} 
) 
public class AuthorizationFilter implements Filter { 

有沒有人知道如何解決這個問題。

+0

見編輯答案。 –

回答

2

我不喜歡這樣的web過濾,並能正常工作:

// 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); 
    } 

Inspiration

+0

@Wesley Egbertsen對不起,這不是我真正的代碼,現在發佈了......你能否檢查一下它是否也適合你?如果我沒有記錯,我遇到了問題,檢測它是否是一個前面的代碼的ajax請求 –

+1

我已經有一個自己的函數來檢測它是否是一個Ajax請求,我真的接近一個解決方案,但是你的部分代碼,你返回一個部分響應與重定向救了我! 這裏是我如何檢查它是否是一個AJAX請求: 'private boolean isAJAXRequest(ServletRequest request){ \t HttpServletRequest req =(HttpServletRequest)請求; \t返回req.getHeader( 「面請求」)=空 \t \t \t && req.getHeader( 「面-請求」)toLowerCase()的indexOf( 「AJAX」)> -1!。; }' –