开发一个web应用程序,现在处理一种情况:
如果用户登录时间过长,session失效后,那么用户点击任何事件都会跳转到首页重新登录。
我用了一个拦截器来拦截所有的action。拦截器的实现如下:public class MyInterceptor implements Interceptor
{
public String intercept(ActionInvocation invocation)
throws Exception
{
//为了测试方便,所以直接让它返回
if (true)
{
return "indexPage";
}
}
}struts的配置如下: <interceptors>
<interceptor name="myInterceptor"
class="myTest.MyInterceptor">
</interceptor>
<interceptor-stack name="mydefault">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="myInterceptor" />
</interceptor-stack>
</interceptors> <!-- 定义默认拦截器 -->
<default-interceptor-ref name="mydefault" />
<global-results>
<result name="indexPage" type="redirect">/login.jsp</result>
</global-results>
<!-- action都是这种没有返回值的,后台的数据直接通过http发送到前台 -->
<!-- 我要拦截这种action -->
<action name="myAction" class="TestAction" method="myTestAction"></action>我测试下了,貌似<global-results> 只能拦截下面这种有返回值的action。<action name="myAction" class="TestAction" method="myTestAction">
<result name="success">/index.jsp</result>
</action>如何实现拦截这种action,并让操作的页面跳转到一个指定的页面。
<action name="myAction" class="TestAction" method="myTestAction"></action>
如果用户登录时间过长,session失效后,那么用户点击任何事件都会跳转到首页重新登录。
我用了一个拦截器来拦截所有的action。拦截器的实现如下:public class MyInterceptor implements Interceptor
{
public String intercept(ActionInvocation invocation)
throws Exception
{
//为了测试方便,所以直接让它返回
if (true)
{
return "indexPage";
}
}
}struts的配置如下: <interceptors>
<interceptor name="myInterceptor"
class="myTest.MyInterceptor">
</interceptor>
<interceptor-stack name="mydefault">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="myInterceptor" />
</interceptor-stack>
</interceptors> <!-- 定义默认拦截器 -->
<default-interceptor-ref name="mydefault" />
<global-results>
<result name="indexPage" type="redirect">/login.jsp</result>
</global-results>
<!-- action都是这种没有返回值的,后台的数据直接通过http发送到前台 -->
<!-- 我要拦截这种action -->
<action name="myAction" class="TestAction" method="myTestAction"></action>我测试下了,貌似<global-results> 只能拦截下面这种有返回值的action。<action name="myAction" class="TestAction" method="myTestAction">
<result name="success">/index.jsp</result>
</action>如何实现拦截这种action,并让操作的页面跳转到一个指定的页面。
<action name="myAction" class="TestAction" method="myTestAction"></action>
<global-forwards>
<forward name="indexPage" path="/index.jsp" />
</global-forwards>如果是一个包(package)里面的全局用:
<global-results>
<result name="indexPage">/login.jsp</result>
</global-results>
package com.chenghui.common.util;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class SessionFilter implements Filter { private FilterConfig config;
private List<String> excludedUrls = new ArrayList<String>(); /**
* 销毁
*/
@Override
public void destroy() {
// TODO Auto-generated method stub } /**
* 执行
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
// TODO Auto-generated method stub
boolean flag = true;
HttpServletRequest req = (HttpServletRequest) request;
String url = req.getRequestURI();
for (String one : excludedUrls) {
if (url.indexOf(one) != -1) {
flag = false;
break;
}
}
if (flag) {
Object obj = req.getSession().getAttribute("loginuser");
if (obj == null) {
HttpServletResponse res = (HttpServletResponse) response;
res.sendRedirect(PropertyManager.getProperty("contextPath") + "/common/validate-session.jsp");
} else {
chain.doFilter(request, response);
}
} else {
chain.doFilter(request, response);
} } /**
* 初始
*/
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
config = arg0;
String s = config.getInitParameter("excludedUrls");
if (s != null) {
excludedUrls = StrUtils.split(s, ",");
}
}}<!-- session Filter -->
<filter>
<filter-name>sessionFilter</filter-name>
<filter-class>com.beyondbit.common.util.SessionFilter</filter-class>
<init-param>
<param-name>excludedUrls</param-name>
<param-value>/login!index.action</param-value> <!--这里指定你自己不需要拦截的action或者其他就可以了,上面的方法 会过滤掉-->
</init-param>
</filter>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>60</session-timeout>
</session-config>