好记性不如烂笔头49-javaWeb框架struts2中的拦截器(4)

来源:互联网 发布:网络综合布线工程师 编辑:程序博客网 时间:2024/05/03 13:57

1、 Struts2的拦截器概述

Struts2拦截器是在访问某个Action或Action的某个方法之前拦截,或者在运行之后处理。Struts2拦截器是可插拔的,拦截器是AOP(Aspect Oriented Programming,面向切面编程)的一种实现
Struts2的拦截器栈(InterceptorStack)就是将拦截器按一定的顺序联结成一条链。在访问被拦截的方法或字段时,Struts2拦截器链中的拦截器就会按其之前定义的顺序被调用。
Struts2规定用户自定义拦截器必须实现com.opensymphony.xwork2.interceptor.Interceptor接口,该接口声明了3个方法
void init();
void destroy();
String intercept(ActionInvocation invocation) throws Exception;
其中,init和destroy方法会在程序开始和结束时各执行一遍,不管使用了该拦截器与否,只要在struts.xml中声明了该Struts2拦截器就会被执行。
intercept方法就是拦截的主体了,每次拦截器生效时都会执行其中的逻辑
不过,struts中又提供了几个抽象类来简化这一步骤
public abstract class AbstractInterceptor implements Interceptor;
public abstract class MethodFilterInterceptor extends AbstractInterceptor;
都是模板方法实现的。
其中AbstractInterceptor提供了init()和destroy()的空实现,使用时只需要覆盖intercept()方法;
而MethodFilterInterceptor则提供了includeMethods和excludeMethods两个属性,用来过滤执行该过滤器的action的方法。可以通过param来加入或者排除需要过滤的方法。

2、 开发前的准备工作

要有一个能运行的struts2环境

3、 直接实现Interceptor接口源代码

package com.struts2;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.Interceptor;/** * 简单实现struts2的拦截器 * @author 范芳铭 */public class EasyInterceptor implements Interceptor {    public void destroy() {        System.out.println("--- 拦截器销毁 ---");    }    public void init() {        System.out.println("--- 拦截器初始化 ---");    }    public String intercept(ActionInvocation invocation) throws Exception {        System.out.println("Action执行前插入 代码");        // 执行目标方法 (调用下一个拦截器, 或执行Action)        final String res = invocation.invoke();        System.out.println("Action执行后插入 代码");        return res;    }}

4、 配合测试用的Action

package com.struts2;import com.opensymphony.xwork2.ActionSupport;/** * 简单实现的action * @author 范芳铭 */public class LoginAction extends ActionSupport {    private static final long serialVersionUID = 7854497526623985504L;    // 主执行方法    public String execute() throws Exception {        System.out.println("---LoginAction 被执行。");        return "success";    }}

5、 配合测试的login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>My JSP 'index.jsp' starting page</title></head>  <body> <form action="aLogin.action" method="post" name="form1">    <table width="392" border="1">      <tr align="center">            <td colspan="2" bgcolor="#FFCCFF"><input type="submit" value="登陆" />             </td>        </tr>    </table>  </form>  </body></html>

6、 Struts.xm的修改


<package name="first" extends="struts-default"><!-- 定义一个package -->
<interceptors>
<interceptor name="MyInterceptor" class="com.struts2.EasyInterceptor"></interceptor>
<interceptor-stack name="myInterceptorStack">
<interceptor-ref name="MyInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<!-- 对action返回结果的配置 -->
<action name="aLogin" class="com.struts2.LoginAction">
<result name="success">/index.jsp</result>
<interceptor-ref name="myInterceptorStack"></interceptor-ref>
</action>
</package>

7、 运行结果

启动中间件,输入 http://127.0.0.1:8080/webStudy/login.jsp
点击“登陆”,控制台打印出:

Action执行前插入 代码
—LoginAction 被执行。
Action执行后插入 代码

1 0