Session ViewExpiredException,无法还原页面

Session ViewExpiredException,无法还原页面,session,jakarta-ee,jsf-2,glassfish,viewexpiredexception,Session,Jakarta Ee,Jsf 2,Glassfish,Viewexpiredexception,我试着在GlassFish 3.1上的Mojarra 2.1.0(带有RichFaces 4)中,按照不同的帖子介绍如何处理ViewExpiredException。但是我在web.xml中定义的内容没有任何效果。我使用基于表单的安全性,用户必须登录才能访问内容。我只希望Glassfish(catalina)在会话超时时将用户重定向到一个简单的JSF页面,并提供一个返回登录页面的链接 我总是收到以下错误消息 javax.faces.application.ViewExpiredException

我试着在GlassFish 3.1上的Mojarra 2.1.0(带有RichFaces 4)中,按照不同的帖子介绍如何处理
ViewExpiredException
。但是我在
web.xml
中定义的内容没有任何效果。我使用基于表单的安全性,用户必须登录才能访问内容。我只希望Glassfish(catalina)在会话超时时将用户重定向到一个简单的JSF页面,并提供一个返回登录页面的链接

我总是收到以下错误消息

javax.faces.application.ViewExpiredException: viewId:/app_user/activity/List.xhtml - View /app_user/activity/List.xhtml could not be restored.
at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:202)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:113)
当用户会话超时并在服务器日志中捕获异常时,谁可以重定向用户

你好,克里斯

layout.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:rich="http://richfaces.org/rich">

    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <!-- Had no effect
        <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
        <meta http-equiv="Pragma" content="no-cache" />
        <meta http-equiv="Expires" content="0" />
        -->      
        <h:outputStylesheet name="css/default.css"/>
        <h:outputStylesheet name="css/cssLayout.css"/>

        <title>
            <h:outputText value="Partner Tapestry - " /> <ui:insert name="title">Browser Title</ui:insert>
        </title>
    </h:head>

    <h:body>
        <div id="top" >
            <ui:insert name="top">Top Default</ui:insert>
        </div>

        <div id="messages">
            <rich:messages id="messagePanel"/>
        </div>

        <div id="content">
            <ui:insert name="content">Content Default</ui:insert>
        </div>
    </h:body>
</html>
...
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>

<session-config>
    <session-timeout>
        2
    </session-timeout>
</session-config>
<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/faces/resources/sessionExpired.xhtml</location>
</error-page>
...
<ui:composition template="/resources/masterLayout.xhtml"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:a4j="http://richfaces.org/a4j">

    <ui:define name="title">
        <h:outputText value="Login"></h:outputText>
    </ui:define>
    <ui:define name="top">
        <h:outputText value="Welcome" />
    </ui:define>

    <ui:define name="content">         
        <h:form>
            <h:panelGrid columns="3">
                <h:outputLabel for="username" value="Username" />
                <h:inputText id="username" value="#{userController.userAuthentication}" />
                <br />
                <h:outputLabel for="password" value="Password" />
                <h:inputSecret id="password" value="#{userController.passwordAuthentication}" />
                <br />
                <h:outputText value=" " />
                <a4j:commandButton action="#{userController.login()}" value="Login"/>
            </h:panelGrid>
        </h:form>
    </ui:define>
</ui:composition>
<ui:composition template="/resources/masterLayout.xhtml"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:a4j="http://richfaces.org/a4j">

    <ui:define name="title">
        <h:outputText value="Session Exired"></h:outputText>
    </ui:define>
    <ui:define name="top">
        <h:outputText value="Session expired, please login again" />
    </ui:define>

    <ui:define name="content"> 
        <h:form>           
            <a4j:commandLink action="/resources/login?faces-redirect=true" value="Login" />
        </h:form>
    </ui:define>
</ui:composition>

浏览器标题
最高违约
内容默认值
web.xml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:rich="http://richfaces.org/rich">

    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <!-- Had no effect
        <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
        <meta http-equiv="Pragma" content="no-cache" />
        <meta http-equiv="Expires" content="0" />
        -->      
        <h:outputStylesheet name="css/default.css"/>
        <h:outputStylesheet name="css/cssLayout.css"/>

        <title>
            <h:outputText value="Partner Tapestry - " /> <ui:insert name="title">Browser Title</ui:insert>
        </title>
    </h:head>

    <h:body>
        <div id="top" >
            <ui:insert name="top">Top Default</ui:insert>
        </div>

        <div id="messages">
            <rich:messages id="messagePanel"/>
        </div>

        <div id="content">
            <ui:insert name="content">Content Default</ui:insert>
        </div>
    </h:body>
</html>
...
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>

<session-config>
    <session-timeout>
        2
    </session-timeout>
</session-config>
<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/faces/resources/sessionExpired.xhtml</location>
</error-page>
...
<ui:composition template="/resources/masterLayout.xhtml"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:a4j="http://richfaces.org/a4j">

    <ui:define name="title">
        <h:outputText value="Login"></h:outputText>
    </ui:define>
    <ui:define name="top">
        <h:outputText value="Welcome" />
    </ui:define>

    <ui:define name="content">         
        <h:form>
            <h:panelGrid columns="3">
                <h:outputLabel for="username" value="Username" />
                <h:inputText id="username" value="#{userController.userAuthentication}" />
                <br />
                <h:outputLabel for="password" value="Password" />
                <h:inputSecret id="password" value="#{userController.passwordAuthentication}" />
                <br />
                <h:outputText value=" " />
                <a4j:commandButton action="#{userController.login()}" value="Login"/>
            </h:panelGrid>
        </h:form>
    </ui:define>
</ui:composition>
<ui:composition template="/resources/masterLayout.xhtml"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:a4j="http://richfaces.org/a4j">

    <ui:define name="title">
        <h:outputText value="Session Exired"></h:outputText>
    </ui:define>
    <ui:define name="top">
        <h:outputText value="Session expired, please login again" />
    </ui:define>

    <ui:define name="content"> 
        <h:form>           
            <a4j:commandLink action="/resources/login?faces-redirect=true" value="Login" />
        </h:form>
    </ui:define>
</ui:composition>
。。。
Facesservlet
/面孔/*
2.
javax.faces.application.ViewExpiredException
/faces/resources/sessionExpired.xhtml
...
login.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:rich="http://richfaces.org/rich">

    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <!-- Had no effect
        <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
        <meta http-equiv="Pragma" content="no-cache" />
        <meta http-equiv="Expires" content="0" />
        -->      
        <h:outputStylesheet name="css/default.css"/>
        <h:outputStylesheet name="css/cssLayout.css"/>

        <title>
            <h:outputText value="Partner Tapestry - " /> <ui:insert name="title">Browser Title</ui:insert>
        </title>
    </h:head>

    <h:body>
        <div id="top" >
            <ui:insert name="top">Top Default</ui:insert>
        </div>

        <div id="messages">
            <rich:messages id="messagePanel"/>
        </div>

        <div id="content">
            <ui:insert name="content">Content Default</ui:insert>
        </div>
    </h:body>
</html>
...
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>

<session-config>
    <session-timeout>
        2
    </session-timeout>
</session-config>
<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/faces/resources/sessionExpired.xhtml</location>
</error-page>
...
<ui:composition template="/resources/masterLayout.xhtml"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:a4j="http://richfaces.org/a4j">

    <ui:define name="title">
        <h:outputText value="Login"></h:outputText>
    </ui:define>
    <ui:define name="top">
        <h:outputText value="Welcome" />
    </ui:define>

    <ui:define name="content">         
        <h:form>
            <h:panelGrid columns="3">
                <h:outputLabel for="username" value="Username" />
                <h:inputText id="username" value="#{userController.userAuthentication}" />
                <br />
                <h:outputLabel for="password" value="Password" />
                <h:inputSecret id="password" value="#{userController.passwordAuthentication}" />
                <br />
                <h:outputText value=" " />
                <a4j:commandButton action="#{userController.login()}" value="Login"/>
            </h:panelGrid>
        </h:form>
    </ui:define>
</ui:composition>
<ui:composition template="/resources/masterLayout.xhtml"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:a4j="http://richfaces.org/a4j">

    <ui:define name="title">
        <h:outputText value="Session Exired"></h:outputText>
    </ui:define>
    <ui:define name="top">
        <h:outputText value="Session expired, please login again" />
    </ui:define>

    <ui:define name="content"> 
        <h:form>           
            <a4j:commandLink action="/resources/login?faces-redirect=true" value="Login" />
        </h:form>
    </ui:define>
</ui:composition>



最后sessionExpire.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:rich="http://richfaces.org/rich">

    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <!-- Had no effect
        <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
        <meta http-equiv="Pragma" content="no-cache" />
        <meta http-equiv="Expires" content="0" />
        -->      
        <h:outputStylesheet name="css/default.css"/>
        <h:outputStylesheet name="css/cssLayout.css"/>

        <title>
            <h:outputText value="Partner Tapestry - " /> <ui:insert name="title">Browser Title</ui:insert>
        </title>
    </h:head>

    <h:body>
        <div id="top" >
            <ui:insert name="top">Top Default</ui:insert>
        </div>

        <div id="messages">
            <rich:messages id="messagePanel"/>
        </div>

        <div id="content">
            <ui:insert name="content">Content Default</ui:insert>
        </div>
    </h:body>
</html>
...
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>

<session-config>
    <session-timeout>
        2
    </session-timeout>
</session-config>
<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/faces/resources/sessionExpired.xhtml</location>
</error-page>
...
<ui:composition template="/resources/masterLayout.xhtml"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:a4j="http://richfaces.org/a4j">

    <ui:define name="title">
        <h:outputText value="Login"></h:outputText>
    </ui:define>
    <ui:define name="top">
        <h:outputText value="Welcome" />
    </ui:define>

    <ui:define name="content">         
        <h:form>
            <h:panelGrid columns="3">
                <h:outputLabel for="username" value="Username" />
                <h:inputText id="username" value="#{userController.userAuthentication}" />
                <br />
                <h:outputLabel for="password" value="Password" />
                <h:inputSecret id="password" value="#{userController.passwordAuthentication}" />
                <br />
                <h:outputText value=" " />
                <a4j:commandButton action="#{userController.login()}" value="Login"/>
            </h:panelGrid>
        </h:form>
    </ui:define>
</ui:composition>
<ui:composition template="/resources/masterLayout.xhtml"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:a4j="http://richfaces.org/a4j">

    <ui:define name="title">
        <h:outputText value="Session Exired"></h:outputText>
    </ui:define>
    <ui:define name="top">
        <h:outputText value="Session expired, please login again" />
    </ui:define>

    <ui:define name="content"> 
        <h:form>           
            <a4j:commandLink action="/resources/login?faces-redirect=true" value="Login" />
        </h:form>
    </ui:define>
</ui:composition>

对于同步POST请求,您的方法应该可以很好地工作。但是,您使用的是异步(ajax)POST请求。ajax请求上的异常需要通过JavaScript中的
jsf.ajax.addOnError
init函数或自定义函数来处理

另见

通过以下链接解决了该问题,该链接介绍了如何实现。这是一个不错的解决方案,您可以有一个异常导航规则,但可能有点长。谢谢你的帮助@BalusC

这也包括在我的答案的第二个“请参阅”链接的问题中。