自定义拦截器

来源:互联网 发布:商家如何入驻淘宝 编辑:程序博客网 时间:2024/05/11 20:07

两种方式:
方式一:
这里写图片描述
ActionInvocation既包括action的信息也包括拦截器的信息,通过它可以获取action的状态或者说拦截器的状态信息。intercept方法返回一个字符串(result),这个返回值就是逻辑视图名。
此方式较麻烦,因为需要实现init和destroy方法。


方式二:
这里写图片描述
AbstractInterceptor:抽象拦截器类,本身也是实现Interceptor接口。
此方法更常用。


综上:自定义拦截器的步骤

  1. 创建一个类继承AbstractInterceptor,并实现intercept方法,在这个方法中进行拦截操作,当执行action时会自动调用这个intercept方法。
  2. 在struts.xml文件中进行配置,首先要注册拦截器,然后在相应的action中引用定义的拦截器。

例:访问Action并计算执行Action花费的时间:分别得到执行action之前的时间和执行action之后的时间,两者相减即可。
创建web项目struts-timerInterceptor,并导入相应的struts2包。
首页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>

web.xml:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>struts-timerInterceptor</display-name>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list>    <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></web-app>

创建action->TimerAction:

package com.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;    }}

点击首页链接跳转成功页面success.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>My JSP 'success.jsp' starting page</title>  </head>    <body>   访问action成功!  </body></html>

创建拦截器TimerInterceptor.java:

package com.interceptor;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;/** * 这个拦截器的作用:计算执行Action花费的时间 * */public class TimerInterceptor extends AbstractInterceptor {    @Override    public String intercept(ActionInvocation invocation) throws Exception {        //1.执行Action之前的时间        long start=System.currentTimeMillis(); //获得当前毫秒值        //2.执行下一个拦截器,如果已经是最后一个拦截器,则执行目标Action        String result=invocation.invoke(); //调用ActionInvocation里的下一个拦截器,返回的字符串是结果视图        //3.在Action之后,统计时间        long end=System.currentTimeMillis();        System.out.println("执行Action花费的时间:"+(end-start)+"ms");        return result; //intercept方法返回一个字符串(result),这个返回值就是逻辑视图名        }}

在struts.xml中配置action和配置拦截器:

<?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>    <constant name="struts.enable.DynamicMethodInvocation" value="false" />    <constant name="struts.devMode" value="true" />    <package name="default" namespace="/" extends="struts-default">        <!-- 注册拦截器 -->        <interceptors>            <interceptor name="mytimer" class="com.interceptor.TimerInterceptor"></interceptor>        </interceptors>        <action name="timer" class="com.action.TimerAction">            <result>/success.jsp</result>            <!-- 引用已注册的拦截器 -->            <interceptor-ref name="mytimer"></interceptor-ref>        </action>    </package></struts>

运行:localhost:8080/struts-timerInterceptor/
这里写图片描述
点击链接:
这里写图片描述
控制台输出文字信息并显示执行action的时间:
这里写图片描述

0 0
原创粉丝点击