javax.faces.application.ViewExpiredException: View could not be restored

前端 未结 10 2489
自闭症患者
自闭症患者 2020-11-21 05:54

I have written simple application with container-managed security. The problem is when I log in and open another page on which I logout, then I come back to first page and I

10条回答
  •  独厮守ぢ
    2020-11-21 06:40

    When our page is idle for x amount of time the view will expire and throw javax.faces.application.ViewExpiredException to prevent this from happening one solution is to create CustomViewHandler that extends ViewHandler and override restoreView method all the other methods are being delegated to the Parent

    import java.io.IOException;
    import javax.faces.FacesException;
    import javax.faces.application.ViewHandler;
    import javax.faces.component.UIViewRoot;
    import javax.faces.context.FacesContext;
    import javax.servlet.http.HttpServletRequest;
    
    public class CustomViewHandler extends ViewHandler {
        private ViewHandler parent;
    
        public CustomViewHandler(ViewHandler parent) {
            //System.out.println("CustomViewHandler.CustomViewHandler():Parent View Handler:"+parent.getClass());
            this.parent = parent;
        }
    
        @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;
        }
    
        @Override
        public Locale calculateLocale(FacesContext facesContext) {
            return parent.calculateLocale(facesContext);
        }
    
        @Override
        public String calculateRenderKitId(FacesContext facesContext) {
            String renderKitId = parent.calculateRenderKitId(facesContext);
            //System.out.println("CustomViewHandler.calculateRenderKitId():RenderKitId: "+renderKitId);
            return renderKitId;
        }
    
        @Override
        public UIViewRoot createView(FacesContext facesContext, String viewId) {
            return parent.createView(facesContext, viewId);
        }
    
        @Override
        public String getActionURL(FacesContext facesContext, String actionId) {
            return parent.getActionURL(facesContext, actionId);
        }
    
        @Override
        public String getResourceURL(FacesContext facesContext, String resId) {
            return parent.getResourceURL(facesContext, resId);
        }
    
        @Override
        public void renderView(FacesContext facesContext, UIViewRoot viewId) throws IOException, FacesException {
            parent.renderView(facesContext, viewId);
        }
    
        @Override
        public void writeState(FacesContext facesContext) throws IOException {
            parent.writeState(facesContext);
        }
    
        public ViewHandler getParent() {
            return parent;
        }
    
    }   
    

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

    
        com.demo.CustomViewHandler
    
    

    Thanks for the original answer on the below link: http://www.gregbugaj.com/?p=164

提交回复
热议问题