您的位置:首页 > 编程语言 > Java开发

struts2 session拦截器

2015-07-03 02:43 357 查看
今天发现在用SessionFilter过滤session时发现servletResponse.sendRedirect(path+"/login.jsp");有时并不能跳转,可改用拦截器实现。

在struts.xml中增加:

Java代码


<!-- 定义一个author拦截器,并定义一个默认拦截器 -->
<interceptors>
<interceptor name="authorLogin" class="com.rating.joyintech.action.AuthorizationInterceptor"/>
<interceptor-stack name="myStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="authorLogin"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack"/>
<global-results>
<result name="noSession">/login.jsp</result>
</global-results>

对应的拦截器代码如下:

Java代码


package com.rating.joyintech.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class AuthorizationInterceptor extends AbstractInterceptor{

@Override
public String intercept(ActionInvocation ai) throws Exception {
// TODO Auto-generated method stub
Map session = ai.getInvocationContext().getSession();
HttpServletRequest request = ServletActionContext.getRequest();
if(session.get("userInfo")==null && request.getRequestURI().indexOf("login")==-1){//login.action不拦截
return "noSession";

}else{
return ai.invoke();
}
}

}

转载自:http://corejava5.iteye.com/blog/1213725
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: