3

我的 h:commandButton "Login" 有问题:当我使用 @ViewScoped 并按下此按钮时,会出现 ViewExpiredException,但是当我使用 @SessionScoped 时,没有任何错误。

堆栈跟踪:

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)

我的页面:

            <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>

这是我的 RegisterController 类:

@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;

}
4

2 回答 2

9

您的具体问题是因为您的视图范围 bean 不可序列化,因此 MyFaces 无法将其保存在视图状态中。默认情况下,MyFaces 序列化会话中的整个状态,而不是仅引用会话中的状态并让容器在必要时对其进行序列化。

基本上有2个解决方案:

  1. 让您的视图范围 bean 实现Serializable

  2. 告诉 MyFaces 不要序列化会话中的整个视图状态。

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

    请注意,每当您重新启动服务器时,视图范围的 bean 仍然会丢失。

于 2013-09-13T16:58:07.190 回答
2

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

JSF2.1 的 MyFaces 实现org.apache.myfaces.SERIALIZE_STATE_IN_SESSION默认为true,而 Mojarra 则com.sun.faces.serializeServerState默认为false.


从 JSF2.2 开始,这将被标准化javax.faces.SERIALIZE_SERVER_STATE,默认为false.

<context-param>
    <param-name>javax.faces.SERIALIZE_SERVER_STATE</param-name>
    <param-value>true</param-value>
</context-param>
于 2013-11-02T15:55:12.717 回答