自定义拦截器分析
来源:互联网 发布:淘宝网购买泓铭软件 编辑:程序博客网 时间:2024/06/06 14:16
在Struts2 中很多功能都是由拦截器完成的比如 模型驱动,动态参数封装。 它是AOP 编程思想的一种应用形式。
类似于servlet 中的过滤器 比如有 1 ,2 3, 3个过滤器他的执行顺序是 先根据注册的顺序执行 1 ,2 ,3 过滤器 然后进入action 再进入到jSP 最后再执行 3,2,1 完之后显示 页面。
进入 ServletConfigInterceptor 类看到 继承于AbstractInterceptor 接下来进到
public abstract class AbstractInterceptor implements Interceptor { /** * Does nothing */ public void init() { } /** * Does nothing */ public void destroy() { } /** * Override to handle interception */ public abstract String intercept(ActionInvocation invocation) throws Exception;}看到代码是这样的 进入Interceptor 看到源码如下
public interface Interceptor extends Serializable { /** * Called to let an interceptor clean up any resources it has allocated. */ void destroy(); /** * Called after an interceptor is created, but before any requests are processed using * {@link #intercept(com.opensymphony.xwork2.ActionInvocation) intercept} , giving * the Interceptor a chance to initialize any needed resources. */ void init(); /** * Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the * request by the {@link ActionInvocation} or to short-circuit the processing and just return a String return code. * * @param invocation the action invocation * @return the return code, either returned from {@link ActionInvocation#invoke()}, or from the interceptor itself. * @throws Exception any system-level error, as defined in {@link com.opensymphony.xwork2.Action#execute()}. */ String intercept(ActionInvocation invocation) throws Exception;}看到3个方法 一个是初始化 一个的销毁 中间的执行过滤。 就可以猜到这个类大概是一个单列模式。
既然struts2 的过滤器 是 继承于 AbstractInterceptor 我们就可以自定义一个过滤器
可以直接继承于AbstractInterceptor 但是一般不会这么做 在开发中可以直接继承于MethodFilterInterceptor 因为过滤器的作用是过滤 用户发出的请求 这个类的作用就是 能手动控制 哪些请求可以过滤哪些 不能过滤。
新建一个过滤器类 CheckLoginInterceptor 继承于MethodFilterInterceptor 实现 doIntercept
接下来 修改配置文件定义一个全局结果视图
<global-results><!-- 全局结果视图 -->
<result name="input">/login.jsp</result>
</global-results>
所有返回input 的请求都会跳转到login.jsp
使用 interceptors标签创建一个过滤器组
把我们自定义的过滤器 加进去
<!-- 申明自定义拦截器 --> <interceptors> <interceptor name="checkLogin" class="com.heima.interceptor.CheckLoginInterceptor"></interceptor> <interceptor-stack name="myDefault"> <!-- 创建过滤器组 --> <interceptor-ref name="defaultStack"> <!-- STUTRS2 默认过滤器 --> </interceptor-ref> <interceptor-ref name="checkLogin"> <!-- 把自定义的过滤器加入到 群组中 --> </interceptor-ref> </interceptor-stack> </interceptors>
需要默认所有动作都使用过滤器
<!-- 默认所有action 都会执行 拦截器 -->
<default-interceptor-ref name="myDefault">
</default-interceptor-ref>
如果有部分动作不需要此过滤器 就可以直接使用
<action name="login" class="com.heima.action.DemoAction" method="login"> <result type="redirectAction">goMain</result> <interceptor-ref name="myDefault"> <param name="checkLogin.excludeMethods">login</param><!-- 这里要用过滤器的引用 和方法名 然后注入 方法 --> </interceptor-ref> </action>
大概就是这样了 第一次学这个 还需要多多联系
- 自定义拦截器分析
- struts2之自定义拦截器及拦截器生命周期分析
- struts2自定义拦截器实现的分析说明附带代码
- struts2 自定义拦截器
- 自定义拦截器(转)
- Struts2自定义拦截器
- 自定义拦截器
- Struts2自定义拦截器
- strust2自定义拦截器
- 自定义时间拦截器
- struts2自定义拦截器
- Struts2自定义拦截器
- 拦截器(自定义)
- Struts2自定义拦截器
- Struts2自定义拦截器
- 自定义异常拦截器
- Struts2自定义拦截器
- struts2自定义拦截器
- 海豚蓝牙ASIO驱动程序使用说明
- 好久没来了,测试一下现在的博客
- PHP实现冒泡排序和快速排序
- 函数的传入和传出参数
- uiweb实现缓存
- 自定义拦截器分析
- 笔记
- java常见的120道面试题第三季
- 使用sz rz命令在windows和linux系统之间传文件
- 将u-boot-2016.11移植到TINY4412
- 神经网络和深度学习
- ClustrixDB在centos上的安装
- 你好2017
- caffe-windows下使用pycaffe绘制网络图