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

前端 未结 10 2484
自闭症患者
自闭症患者 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:35

    I ran into this problem myself and realized that it was because of a side-effect of a Filter that I created which was filtering all requests on the appliation. As soon as I modified the filter to pick only certain requests, this problem did not occur. It maybe good to check for such filters in your application and see how they behave.

    0 讨论(0)
  • 2020-11-21 06:37

    You coud use your own custom AjaxExceptionHandler or primefaces-extensions

    Update your faces-config.xml

    ...
    <factory>
      <exception-handler-factory>org.primefaces.extensions.component.ajaxerrorhandler.AjaxExceptionHandlerFactory</exception-handler-factory>
    </factory>
    ...
    

    Add following code in your jsf page

    ...
    <pe:ajaxErrorHandler />
    ...
    
    0 讨论(0)
  • 2020-11-21 06:37

    I was getting this error : javax.faces.application.ViewExpiredException.When I using different requests, I found those having same JsessionId, even after restarting the server. So this is due to the browser cache. Just close the browser and try, it will work.

    0 讨论(0)
  • 2020-11-21 06:38

    First what you have to do, before changing web.xml is to make sure your ManagedBean implements Serializable:

    @ManagedBean
    @ViewScoped
    public class Login implements Serializable {
    }
    

    Especially if you use MyFaces

    0 讨论(0)
  • 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

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

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

    0 讨论(0)
  • 2020-11-21 06:44

    I add the following configuration to web.xml and it got resolved.

    <context-param>
        <param-name>com.sun.faces.numberOfViewsInSession</param-name>
        <param-value>500</param-value>
    </context-param>
    <context-param>
        <param-name>com.sun.faces.numberOfLogicalViews</param-name>
        <param-value>500</param-value>
    </context-param>
    
    0 讨论(0)
提交回复
热议问题