3

I have problem with my h:commandButton "Login": when I use @ViewScoped and push this button there is ViewExpiredException, but when I use @SessionScoped, there isn't any error.

Stack Trace:

javax.faces.application.ViewExpiredException: /pages/register.xhtmlNo saved view state could be found for the view identifier: /pages/register.xhtml
at org.apache.myfaces.lifecycle.RestoreViewExecutor.execute(RestoreViewExecutor.java:132)
at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:170)
at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:45)
at org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:45)
at org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:45)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)

my page:

            <h:form>
                <h:panelGrid columns="2" >
                    <h:outputLabel value="Login:"/>
                    <h:inputText value="#{registerController.registerLog}"/>
                    <h:outputLabel value="#{msg.password}"/>
                    <h:inputSecret id="pass" value=""/>

                    <h:column/>
                    <h:commandButton value="Login" action="#{registerController.login}"/> 

                </h:panelGrid> 
            </h:form>

and this is my RegisterController class:

@ManagedBean
@ViewScoped
public class RegisterController {

private String registerLog = "";
private String registerPass = "";


/**
 * Creates a new instance of RegisterController
 */
public RegisterController() {
}

//getters, setters

public String login(){

    return null;

}
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
kuba44
  • 1,670
  • 9
  • 29
  • 56
  • did you check http://stackoverflow.com/questions/10535227/no-saved-view-state-could-be-found-for-the-view-identifier? – noone Sep 13 '13 at 16:00
  • yes, but it isn't help – kuba44 Sep 13 '13 at 16:14
  • Let your bean `implements Serializable` and retry. Does that fix the problem or not? The exception is by the way misleading for that problem, but MyFaces by default always serializes the view state even with server side state saving turned on, so you never know. – BalusC Sep 13 '13 at 16:36
  • yes, this fix the problem. Thank you :) But what is the problem ? Why should i implements Serializable ? – kuba44 Sep 13 '13 at 16:43
  • I am getting this error and my bean does implement Serializable... thoughts? – Seth M. Apr 29 '14 at 21:16
  • @SethM. - Did you succeeded to fix it? It happened to me and I can't figure it out. I found this - https://myfaces.apache.org/wiki/core/user-guide/jsf-and-myfaces-howtos/managing-errors---infos---warnings/viewexpiredexception-and-session-expiry.html it describes my case, but I have no idea how to fix it from this article. – Anton Jan 26 '15 at 13:00
  • @Anton look at BalusC answer below – kuba44 Jan 26 '15 at 13:20
  • 1
    @kuba44 Well, all of it was done after first googling :) If you'll look at link I posted it brings a bit different scenario. I don't know if it deserves another post on SO, but I solved it by adding httpsessionfilter on timeout to redirect me to login and than not to restore the viewstate. – Anton Jan 26 '15 at 13:32

2 Answers2

9

Your concrete problem is caused because your view scoped bean is not serializable and hence MyFaces is not able to save it in the view state. MyFaces by default serializes the whole state in session instead of just referencing the state in session and having the container to serialize it if necessary.

There are basically 2 solutions:

  1. Let your view scoped bean implement Serializable.

  2. Tell MyFaces to not serialize the whole view state in session.

    <context-param>
        <param-name>org.apache.myfaces.SERIALIZE_STATE_IN_SESSION</param-name>
        <param-value>false</param-value>
    </context-param>
    

    Note that the view scoped bean will still be lost whenever you restart the server.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
1

http://arjan-tijms.omnifaces.org/p/jsf-22.html#1127

MyFaces implementation of JSF2.1 had org.apache.myfaces.SERIALIZE_STATE_IN_SESSION default to true, wheres Mojarra had com.sun.faces.serializeServerState default to false.


From JSF2.2 onwards this will be standardised via javax.faces.SERIALIZE_SERVER_STATE which defaults to false.

<context-param>
    <param-name>javax.faces.SERIALIZE_SERVER_STATE</param-name>
    <param-value>true</param-value>
</context-param>
Arjan Tijms
  • 36,666
  • 12
  • 105
  • 134
Chacko Mathew
  • 1,301
  • 1
  • 17
  • 39