Struts 2 会话超时 [英] Struts 2 Session TimeOut
问题描述
我是 struts 的新手.我正在使用 Struts2.请任何人告诉我如何在会话超时后自动重定向jsp页面.
I am new to struts. I am using Struts2. Please any one can tell me how to automatically redirect the jsp page after Session has Timeout.
推荐答案
好吧,您需要创建一种方法来检查会话是否已过期,因为浏览器无法确定会话是否已过期.你需要有以下步骤
Well You need to create a way to check if the session has expired or not since Browser has no way to find out if the Session has expired or not. You need to have following steps
在您的 web.xml 中定义会话超时.
Define session time out in your web.xml like.
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
绕过 Struts2 的一种简单方法是创建一个 Interceptor 并检查会话有效性,如果会话已过期,您可以将用户重定向回您指定的 jsp 页面.这是示例拦截器的快速视图
One of easy way around Struts2 is to create an Interceptor and check Session validity and if session has expired you can redirect user back to your specified jsp page.Here is a quick view of a sample interceptor
public class SessionInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
Map<String,Object> session = invocation.getInvocationContext().getSession();
if(session.isEmpty())
return "session"; // session is empty/expired
return invocation.invoke();
}
最后你需要通过在 struts.xml 文件中声明它来告诉 Stuts2 你想使用这个拦截器
Finally you need to tell Stuts2 that you want to use this interceptor by declaring it in struts.xml file
<interceptor name="session" class="myapp.interceptor.SessionInterceptor" />
<interceptor-stack name="sessionExpirayStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="session"/>
</interceptor-stack>
现在你需要在你的动作中使用这个堆栈声明
Now all you need to use this stack declaration in your actions
<action name="myAction" class="myClass">
<interceptor-ref name="sessionExpirayStack" />
<result name="success">success.jsp</result>
<result name="session">sessionexpired.jsp</result>
</action>
或者,您可以为 *session*
声明一个全局结果,以便使用将全局重定向到同一页面.
alternative you can declare a global result for *session*
so that use will be redirected to the same page globally.
对于 Struts2 之外的方式,您可以选择创建一个 Servlet 过滤器并且可以将您的会话检查代码放在过滤器中.所有您需要实现 javax.servlet.Filter
接口
For way outside Struts2 you have the option to create a Servlet filter and can place you session check code inside the filter.All you need to impliment javax.servlet.Filter
interface
public final CheckSession impliments Filter{
private FilterConfig filterConfig = null;
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
public void destroy() {
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain
chain) {
// Put your logic here to check session
}
}
对于自动重定向,您必须通过某种 Ajax 调用不断检查会话
For Auto-redirect you have to keep on checking the session by some sort of Ajax call
这篇关于Struts 2 会话超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!