日期:2014-05-17 浏览次数:20767 次
public class Filter extends AbstractInterceptor {
private List<String> list = new ArrayList<String>();
@Override
public String intercept(ActionInvocation actioninvocation) throws Exception {
// String servletPath = request.getServletPath();
// ActionContext context = actioninvocation.getInvocationContext();
// HttpServletRequest request =
// (HttpServletRequest)context.get("com.opensymphony.xwork2.dispatcher.HttpServletRequest");
System.out.println("进入过滤器");
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
String servletPath = request.getServletPath();// 这里是得到你要访问的资源url
for (String url : list) {
if (servletPath.indexOf(url) > -1) {// 需要过滤
System.out.println("进入session过滤器->访问路径为[" + servletPath + "]");
if (request.getSession().getAttribute("user") == null) {// session不存在需要拦截
request.setAttribute("hint", "您还没有登录或登录已超时,请重新登录,然后再刷新本功能!");
request.getRequestDispatcher("/nosession.jsp")
.forward(request, response);
break;
}
break;
}
}
return actioninvocation.invoke();
}
@Override
public void init() {
System.out.println("初始化滤器");
list.clear();
list.add("/user/userAction_login.do");
}
<!-- 自定义拦截器 -->
<package name="pri" namespace="/pri" extends="struts-default">
<interceptors><!--第一步: 声明拦截器; -->
<interceptor name="sessionInterceptor"
class="com.syyx.officemanage.common.utils.Filter" />
<!--第二步: 定义自己的拦截器栈; -->
<interceptor-stack name="myStack">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="sessionInterceptor" />
</interceptor-stack>
</interceptors><!--第三步 指定该packag默认的拦截器栈; -->
<default-interceptor-ref name="myStack" />
</package>
<!-- 要拦截的页面 -->
<package name="actionInterceptor" extends="struts-default">
<action name="login_*" method="{1}"
class="com.syyx.officemanage.user.action.UserAction">
<!-- UserAction 里有login()方法 -->
<result>/login.jsp</result>
</action>
</package>
<constant name="struts.action.extension" value="do" />
<include file="struts_*.xml"></include>