Struts2自定义拦截器
来源:互联网 发布:unity3d 角色跳跃 编辑:程序博客网 时间:2024/04/29 20:53
众所周知,Struts2的大部分功能都是通过拦截器实现的,就算是Struts2的启动也是使用Servlet的拦截器启动的,下
面我们来说一下自定义Struts2拦截器,其实只要是学过Servlet的拦截器,就应该明白大概原理,基本上是差不多的。
首先需要写一个类,实现Struts2的指定接口,com.opensymphony.xwork2.interceptor.Interceptor;
package com.bird.action;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.Interceptor;public class PermissionInterceptor implements Interceptor {private static final long serialVersionUID = 1L;public void destroy() {// TODO Auto-generated method stub}public void init() {// TODO Auto-generated method stub}public String intercept(ActionInvocation action) throws Exception {Object value = ActionContext.getContext().getSession().get("user");if(value != null) return action.invoke();ActionContext.getContext().put("message", "您没有权限登录");return "message";}}
和Servlet拦截器类似,主要都是在intercept这个方法上了,这里判断session里面是否有值,如果有值就调用拦截器后
面的Action的方法,这样就完成了拦截器的任务,没有servlet里面的拦截器链。下面的工作就是注册拦截器,在
struts.xml这个配置文件里面这样配置。
<package name="niao" namespace="/t" extends="struts-default"><interceptors><interceptor name="permission" class="com.bird.action.PermissionInterceptor"/><interceptor-stack name="permissionStack"><interceptor-ref name="defaultStack"></interceptor-ref><interceptor-ref name="permission"></interceptor-ref></interceptor-stack></interceptors><global-results><result name="message">/WEB-INF/jsp/hello.jsp</result></global-results><action name="login_*" class="com.bird.action.Login" method="{1}"><interceptor-ref name="permissionStack"/><result name="success">/WEB-INF/jsp/hello.jsp</result></action></package>
这里启用了拦截器栈,这个意思是因为一旦你定义了自己的拦截器就把struts默认的拦截器都给屏蔽掉了,这样的话
很多struts的功能就不能使用了,所以使用了拦截器栈,这样把struts默认的和你自定义的都放在了一个堆栈中,实现
了自定义和默认的拦截器的同时使用,默认堆栈的名称为defalutStack,这个名字可以在struts-default.xml文件里面找
到,不能写错了。然后定义了一个全局信息处理页面,最后在action定义里面直接把拦截器堆栈放到里面去就行了。
总体感觉还是挺简单的,至少和servlet差不多
- struts2 自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- struts2自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- struts2自定义拦截器
- Struts2---自定义拦截器
- struts2自定义拦截器
- struts2自定义拦截器
- Struts2自定义拦截器
- struts2自定义拦截器
- struts2自定义拦截器
- struts2自定义拦截器
- struts2自定义拦截器
- struts2自定义拦截器
- 参数估计的基本概念
- jbpm报错15:44:50.796] couldn't set dynamic validation feature
- Java中字符串比较的注意点
- 非参数估计
- 怎么免费下载studio 2010
- Struts2自定义拦截器
- sharepoint 部署命令
- 关于ldf日志文件大小如何减少?
- Fusionio性能测试与瓶颈分析
- 有关session生命周期如何设置的问题
- linux cp命令介绍
- 【动态规划】poj1458 common subsequence
- 设计模式总结性试题
- hdu 3397 Sequence operation (线段树+区间合并+双Lazy标记)