Redirecting on session timeout in JSF-Richfaces-facelet

こ雲淡風輕ζ 提交于 2019-11-27 13:22:31

This should do it :

<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/sessionExpired.jsf</location>
</error-page>
Oscar Castiblanco

you should put a timeout in your web.xml and register a timeout filter as shown in this thread: Auto-logout in JSF Application in case of ajax, your redirection has to be done like that:

    String facesRequestHeader = httpServletRequest
            .getHeader( "Faces-Request" );

    boolean isAjaxRequest = facesRequestHeader != null
            && facesRequestHeader.equals( "partial/ajax" );

    if( isAjaxRequest )
    {
            String url = MessageFormat.format( "{0}://{1}:{2,number,####0}{3}",
            request.getScheme(), request.getServerName(),
            request.getServerPort(), timeoutPath );

            PrintWriter pw = response.getWriter();
                pw.println( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" );
            pw.println( "<partial-response><redirect url=\"" + url
            + "\"></redirect></partial-response>" );
            pw.flush(););
    }
    else
    {
        httpServletResponse.sendRedirect( timeoutPath );
    }

The solution is to use Richfaces own session expired event.

Add this to the page prone to expire:

<a4j:region>
 <script language="javascript">
 A4J.AJAX.onExpired = function(loc, expiredMsg){
 alert('expired!');
 window.location = "/login.jsf";
 }
 </script>
</a4j:region>

More info can be found at the RichFaces documentation: http://docs.jboss.org/richfaces/latest_3_3_X/en/devguide/html/ArchitectureOverview.html#SessionExpiredHandling

I had some problems when I making A4J requests after session expiration. I put this

<context-param>
    <param-name>com.sun.faces.enableRestoreView11Compatibility</param-name>
    <param-value>true</param-value>
</context-param>

in my web.xml, for me it solves the problem.

Greg

Another solution is to create CustomViewHandler that extends ViewHandler and override restoreView method

@Override
public UIViewRoot restoreView(FacesContext facesContext, String viewId) {
/**
 * {@link javax.faces.application.ViewExpiredException}. This happens only  when we try to logout from timed out pages.
 */
    UIViewRoot root = null; 
    root = parent.restoreView(facesContext, viewId);
    if(root == null) {          
        root = createView(facesContext, viewId);
    }
    return root;
}

Then you need to add it to your faces-config.xml

<view-handler>com.demo.CustomViewHandler</view-handler>

This will prevent you from getting ViewExpiredException's

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!