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

struts2自定义拦截器

2012-09-29 08:10 309 查看
自定义拦截器,在struts.xml文件中使用这个拦截器可以对没登陆的用户进行拦截,还可以防止退出后返回还能进系统的bug

package crm.interceptor;

import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

import crm.bean.Worker;

public class MyInterceptor extends AbstractInterceptor {

/**

* 序列号

*/

private static final long serialVersionUID = 1L;

public String intercept(ActionInvocation invocation) throws Exception {

ServletActionContext.getResponse().setHeader("Pragma", "no-cache");

ServletActionContext.getResponse().setHeader("Cache-Control", "no-store, no-cache, must-revalidate");

ServletActionContext.getResponse().addHeader("Cache-Control", "post-check=0, pre-check=0");

ServletActionContext.getResponse().setHeader("Expires", "0");

//获取session中保存的用户信息

Map<String,Object> session=invocation.getInvocationContext().getSession();

Worker worker=(Worker)session.get("worker");

if(worker==null)

return Action.LOGIN;

else

return invocation.invoke();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: