java.lang.IllegalStateException at com.sun.faces.context.FacesContextImpl.assertNotReleased

前端 未结 1 1735
情话喂你
情话喂你 2020-11-27 08:29

If I press the submit` button error messages are thrown at server log

login.xhtml


      
         


        
相关标签:
1条回答
  • 2020-11-27 08:43

    Your mistake is here:

    @ManagedBean
    @ViewScoped
    public class Login {
    
        private FacesContext fCtx;
    
        public Login() {
            fCtx = FacesContext.getCurrentInstance();
        }
    }
    

    You should never assign FacesContext as instance variable of a view/session/application scoped managed bean and reuse it in different requests, because the current instance of the FacesContext is tied to the current request which makes it inherently request scoped (but preferably also don't so in request scoped beans because of poor style which may be confusing to starters).

    The FacesContext instance is released by end of request. In subsequent requests, the instance obtained in a previous request isn't valid anymore. If it would have worked, then all getter methods on ExternalContext would have returned you the properties (parameters, cookies, headers, etc) of a previous HTTP request and all setter methods whould set to a previous HTTP response which was already committed for long. This all just isn't right.

    You should always obtain it on a per thread local basis (i.e. straight inside the method block). In your specific case, just obtain it straight inside the login() method:

    public String login() {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info: ", getUsername() + ", " + getPassword() + ", "));  
        return "start.xhtml";
    }
    
    0 讨论(0)
提交回复
热议问题