Struts2拦截器浅析(一)

来源:互联网 发布:linux 用户配置文件 编辑:程序博客网 时间:2024/05/02 02:54

拦截器简介

什么是拦截器

    Struts2大多数核心功能是通过拦截器实现的,每个拦截器完成某项功能,如数据转移、类型转换、数据校验等
    拦截器方法在Action执行之前或者之后执行
    拦截器栈
  1.  从结构上看,拦截器栈相当于多个拦截器的组合
  2. 从功能上看,拦截器栈也是拦截器
    拦截器的执行过程是一个递归的过程。类似于Servlet的过滤器。

自定义拦截器

    方式一:实现Interceptor接口
    - void init() : 初始化拦截器所需资源
    - void destroy() : 释放在init()中分配的资源
    - String intercept(ActionInvocation ai) throws Exception
  • 实现拦截器功能
  • 利用ActionInvocation参数获取Action状态
  • 返回result字符串作为逻辑视图
    方式二:继承AbstractInterceptor类
  • 提供了init()和destroy()方法的空实现
  • 只需要实现interceptor方法即可  

拦截器示例

    计算Action的执行时间:执行之后的时间 - 执行之前时间 = 执行Action消耗时间
    实现步骤:
  • 创建拦截器
  • 在配置文件中定义拦截器并引用它

第一个拦截器示例

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">    <filter>        <filter-name>struts2</filter-name>        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>    </filter>    <filter-mapping>        <filter-name>struts2</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <welcome-file-list>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list>    <login-config>        <auth-method>BASIC</auth-method>    </login-config></web-app>
修改页面index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">    <title>计算执行Action花费的时间</title>  </head>  <body>    <a href="timer">访问Action并计算执行Action花费的时间</a>  </body></html>
创建Action

package com.lijy.action;import com.opensymphony.xwork2.ActionSupport;public class TimerAction extends ActionSupport {    @Override    public String execute() throws Exception {        for (int i = 0; i < 10000; i++) {            System.out.println("I love me");        }        return SUCCESS;    }}
创建struts.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <package name="default" namespace="/" extends="struts-default">        <action name="timer" class="com.lijy.action.TimerAction">            <result>/success.jsp</result>        </action>    </package>    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>    <constant name="struts.devMode" value="true"></constant></struts>
测试跳转: http://localhost:8080/HelloStruts2_2/

创建拦截器

package com.lijy.interceptor;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;/** * 计算并执行Action花费的时间 */public class TimerInterceptor extends AbstractInterceptor {    //1.自动调用此方法进行拦截器操作    //2.在struts.xml中进行配置  注册拦截器,引用拦截器    @Override    public String intercept(ActionInvocation invocation) throws Exception {        //1.执行Action之前        long start = System.currentTimeMillis();        //2.执行下一个拦截器,如果已经是最后一个拦截器,则执行目标Action        String result = invocation.invoke();        //3.执行Action之后        long end = System.currentTimeMillis();        System.out.println("执行Action花费的时间:" + (end - start) + "ms");        return result;    }}
在struts.xml中注册拦截器

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <package name="default" namespace="/" extends="struts-default">        <!-- 1.注册拦截器 -->        <interceptors>            <interceptor name="mytimer" class="com.lijy.interceptor.TimerInterceptor"></interceptor>        </interceptors>        <action name="timer" class="com.lijy.action.TimerAction">            <result>/success.jsp</result>            <!-- 2.引用拦截器 -->            <interceptor-ref name="mytimer"></interceptor-ref>        </action>    </package>    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>    <constant name="struts.devMode" value="true"></constant></struts>