0

我最近尝试通过web.xml文件升级系统以使用户会话超时,设置为 10 分钟,Filter重定向到我的登录页面以及PhaseListener跟踪用户活动。

我的登录 bean 在实例化时执行以下操作:

private User user; //this already has getters/setters

public LoginBean() {
    log.info("Instantiating Login Bean...");
    if(!hasSessionData("user")) {
        user = new User();
        Dealer dealer = new Dealer();
        user.setDealer(dealer);
        setSessionData("user",user);
    }       
}

这将打开登录表单并用于User包含用于提交的登录详细信息。但是,如果我在登录屏幕上停留超过 10 分钟(会话过期)并尝试登录,则会收到错误消息。

Aug 01, 2013 10:32:20 AM com.sun.faces.lifecycle.ProcessValidationsPhase execute
WARNING: /Login30FrmView.xhtml @108,92 value="#{loginBean.user.dealerId}": Target Unreachable, 'user' returned null
javax.el.PropertyNotFoundException: /Login30FrmView.xhtml @108,92 value="#{loginBean.user.dealerId}": Target Unreachable, 'user' returned null
    at com.sun.facelets.el.TagValueExpression.getType(TagValueExpression.java:62)
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:92)
    at javax.faces.component.UIInput.getConvertedValue(UIInput.java:942)
    at javax.faces.component.UIInput.validate(UIInput.java:868)
    at javax.faces.component.UIInput.executeValidate(UIInput.java:1072)
    at javax.faces.component.UIInput.processValidators(UIInput.java:672)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1058)
    at javax.faces.component.UIForm.processValidators(UIForm.java:234)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1058)
    at org.ajax4jsf.component.AjaxViewRoot$3.invokeContextCallback(AjaxViewRoot.java:447)
    at org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:240)
    at org.ajax4jsf.component.AjaxViewRoot.processValidators(AjaxViewRoot.java:463)
    at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:265)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:206)
    at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
    at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388)
    at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:125)
    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.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:947)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    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:722)

2013 年 8 月 1 日上午 10:32:20 com.sun.faces.lifecycle.Phase doPhase SEVERE:JSF1054:(阶段 ID:PROCESS_VALIDATIONS 3,视图 ID:/Login30FrmView.xhtml)阶段执行期间引发异常:javax.faces.event .PhaseEvent[来源=com.sun.faces.lifecycle.LifecycleImpl@7d137b]

我想知道为什么当 LoginBean 仍然根据日志初始化时用户返回 null。在会话过期时,user在会话中也过期,所以if(!hasSessionData("user"))应该返回 true 并再次初始化user。但是,如果用户必须在页面加载时重新初始化并且页面超时......怎么办?Filter每当会话被破坏时,我都会重定向到登录,如下所示:

if (isSessionInvalid(httpServletRequest)) {
    String timeoutUrl = httpServletRequest.getContextPath() + "/" + getTimeoutPage();
          httpServletResponse.sendRedirect(timeoutUrl);
          System.out.println("Redirecting to login page");
          return;
          }
    }

但不知何故,我需要在提交表单并且会话过期时触发它,它似乎并没有按原样执行......我对此没有太多经验,所以我想知道这是否可能?会话到期后,如何user再次初始化表单提交?

(请注意,刷新页面会使一切恢复正常,但如果我让可怕的错误消息出现,我会收到很多来自最终用户的投诉。)

编辑:我的登录提交使用 a<h:commandButton>并指向我的LoginBean. 是否有任何方法可以捕获是否已提交,以便在发生上述情况时恢复到登录页面?这可能是一种可能的替代解决方案。我只是真的希望该错误消失,即使它在没有解释的情况下将用户跳回登录。

4

1 回答 1

0

找到解决方案后回答我自己的问题。我只需要将这一行添加到我的登录表单中。

<meta http-equiv="refresh" content="#{session.maxInactiveInterval}"/>

这会在超时之前刷新表单以保留会话,从而确保始终存在user会话中,因为刷新会实例化 bean 并再次加载页面。

于 2013-08-01T03:35:15.270 回答