struts2拦截器的使用经验
来源:互联网 发布:影视制作特效软件 编辑:程序博客网 时间:2024/06/03 16:21
1.在struts.xml文件中配置拦截器:
<interceptors> <interceptor name="actionCheck" class="com.xh.td.interceptor.ManagerInterceptor" ></interceptor> <interceptor-stack name="mystack"> <interceptor-ref name="actionCheck" /> <interceptor-ref name="defaultStack" /> </interceptor-stack> </interceptors> <default-interceptor-ref name="mystack"/>
其中interceptor标签用于定义用户的自定义拦截器,该拦截器类需要继承AbstractInterceptor类(该类在xwork-core.jar中),
interceptor-stack标签用于定义自己的拦截器栈,在其中可以定义一系列的拦截器,在调用的时候,包含在其中的拦截器会从上到下的一次调用,在拦截器栈的最后最好将defaultStack这个拦截器栈引用上,否则容易出问题。在defaultStack拦截器栈中定义了一系列的struts2中的拦截器,具体的用户可以查看default-struts.xml中的定义。
default-interceptor-ref标签用于定义相应包中的默认拦截器,这个默认拦截器会自动的添加到当前包的所有action中。
2.编写拦截器类:
public class MyIntercer extends AbstractInterceptor {private static final long serialVersionUID = 1261275586263865975L;private static final String LOGIN = "login";private static final String NO_ACTION = "noaction";@Overridepublic String intercept(ActionInvocation ai) throws Exception {Object action = ai.getAction();if(action instanceof LoginAction)//判断当前action是否是用于登录的actin类,如果是则返回return ai.invoke();Map<String,Object> session = ActionContext.getContext().getSession();if(null == session.get(SessionStatus.IS_LOGIN)){//用户是否登陆return LOGIN;}if(action instanceof UserAction){//判断当前action是否是用于操作用户信息的actin类if(!UserStatus.SUPER_MANAGER_YES.equals((Integer)session.get(SessionStatus.IS_SUPER_MANAGER))){//是否是超级管理员return NO_ACTION;}}return ai.invoke();}}
在方法intercept中,返回的是视图名。
3.有时候我们可能需要使得一些的action类中的部分方法不被拦截器拦截,这是我们可以通过配置xml文件实现:
<interceptor name="actionCheck" class="com.xh.td.interceptor.ManagerInterceptor" > <param name="excludeMethods">modifyOwnPassword,find*</param> </interceptor>
其中excludeMethods属性中就是不被拦截的方法.。还有一个属性includeMethods用于定义需要被拦截的方法。
但要这两个属性起作用那么拦截器必须要继承类MethodFilterInterceptor,这个类是AbstractInterceptor的子类。
public class ManagerInterceptor extends MethodFilterInterceptor {private static final long serialVersionUID = 1261275586263865975L;private static final String LOGIN = "login";@SuppressWarnings("unused")private static final String ERROR = "error";private static final String NO_ACTION = "noaction";@Overrideprotected String doIntercept(ActionInvocation ai) throws Exception {Object action = ai.getAction();if(action instanceof LoginAction)return ai.invoke();Map<String,Object> session = ActionContext.getContext().getSession();if(null == session.get(SessionStatus.IS_LOGIN)){//用户是否登陆return LOGIN;}if(action instanceof UserAction){if(!UserStatus.SUPER_MANAGER_YES.equals((Integer)session.get(SessionStatus.IS_SUPER_MANAGER))){//是否是超级管理员return NO_ACTION;}}return ai.invoke();}}
- struts2拦截器的使用经验
- Struts2拦截器的使用
- Struts2拦截器的使用
- struts2拦截器的使用
- Struts2拦截器的使用
- Struts2拦截器的使用
- Struts2的拦截器使用
- struts2拦截器的使用
- Struts2拦截器的使用
- Struts2拦截器的使用
- Struts2拦截器的使用
- Struts2拦截器的使用
- STRUTS2拦截器的使用
- Struts2拦截器的使用
- Struts2拦截器的使用
- Struts2拦截器的使用
- Struts2拦截器的使用
- Struts2拦截器的使用
- 计算某个月有多少天的方法
- Flex 4.5.1 IOS 开发系列(1) 开发环境搭建
- Embedded Microcontroller based Computer Numerical Control
- 整理jQuery学习资料---持续更新...
- springMVC_后台自动接收list数据
- struts2拦截器的使用经验
- LeetCode - Distinct Subsequences
- ARM amba总线介绍
- java restful webservice 之MultivaluedMap 例子
- 冒泡排序
- 重载运算符
- JavaScript就这么回事 (JS基础知识整理)
- webConfig 加密与解密
- qt编译mysql驱动-------windows平台和linux平台