© 版权声明:本文为博主原创文章,转载请注明出处

拦截器:

  - Struts2大多数核心功能都是通过拦截器实现的,每个拦截器完成某项功能

  - 拦截器方法在Action执行之前或之后执行

工作原理:

  - 拦截器的执行过程是一个递归的过程

  - action请求-->拦截器1-->拦截器2-->...-->拦截器n-->action方法-->返回结果-->拦截器n-->...-->拦截器2-->拦截器1-->逻辑视图

实现方式:

  - 方式一:实现Interceptor接口

    1) void init():初始化拦截器所需资源

    2) void destroy():释放在init()中分配的资源

    3) String interceptor(ActionInvocation invocation) throw Exception

      · 实现拦截器工鞥呢

      · 利用ActionInvocation参数获取Action动态

      · 返回result字符串作为逻辑视图

  - 方式二:继承AbstractInterceptor类(常用)

    1) 提供了init()和destroy()方法的空实现

    2) 只需要实现interceptor方法

Struts2内置拦截器:

  - params拦截器

    负责将请求参数设置为Action属性

  - staticParams拦截器

    将配置文件中action子元素param参数设置为Action属性

  - servletConfig拦截器

    将源于Servlet API的各种对象注入到Action中,必须实现对应接口

  - fileUpload拦截器

    对文件上传提供支持,将文件和元数据设置到对应的Action属性(对Commons-FileUpload进行封装)

  - exception

    捕获异常,并且将异常映射到用户自定义的错误页面

  - validation拦截器

    调用验证框架进行数据验证

实例:

1.项目结构

2.pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.struts</groupId>
<artifactId>Struts2-TimerInterceptor</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<struts.version>2.5.10</struts.version>
</properties> <dependencies>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency>
</dependencies> <build>
<finalName>Struts2-TimerInterceptor</finalName>
</build> </project>

3.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_0.xsd"
version="3.0" metadata-complete="true"> <!-- 首页 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <!-- Struts2过滤器-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>

4.index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Struts2拦截器示例之时间拦截器</title>
</head>
<body>
<a href="timer">执行Action方法并计算执行时间</a>
</body>
</html>

5.success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>执行成功</title>
</head>
<body>
Action执行成功
</body>
</html>

6.TimerAction.java

package org.struts.action;

import com.opensymphony.xwork2.ActionSupport;

public class TimerAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	@Override
public String execute() throws Exception { for (int i = 0; i <1000; i++) { System.out.println("第" + i + "次循环"); }
return SUCCESS; } }

7.TimerInterceptor.java

package org.struts.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class TimerInterceptor extends AbstractInterceptor { private static final long serialVersionUID = 1L; @Override
public String intercept(ActionInvocation invocation) throws Exception { // 获取当前时间:ms
long start = System.currentTimeMillis();
// 执行下一个拦截器,若已是最后一个拦截器则执行action方法(result为逻辑视图名称)
String result = invocation.invoke();
// 获取当前时间:ms
long end = System.currentTimeMillis();
// 计算action执行时间并输出
System.out.println("action执行时间为:" + (end - start) + "ms");
// 返回逻辑视图
return result; } }

8.struts.xml

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

9.效果预览

参考:http://www.imooc.com/video/9010

Struts2学习之拦截器的更多相关文章

  1. struts2学习(5)拦截器简介以及例子执行过程

    一.拦截器简介: 二.Struts2预定义拦截器&拦截器栈 在执行action之前和之后,拦截器进行了操作: 比如struts-default.xml中就有很多预定义的拦截器:   拦截器栈: ...

  2. struts2学习笔记--拦截器(Interceptor)和登录权限验证Demo

    理解 Interceptor拦截器类似于我们学过的过滤器,是可以在action执行前后执行的代码.是我们做web开发是经常使用的技术,比如权限控制,日志.我们也可以把多个interceptor连在一起 ...

  3. Struts2学习之拦截器栈

    © 版权声明:本文为博主原创文章,转载请注明出处 拦截器栈: - 从结构上看:拦截器栈相当于多个拦截器的组合 - 从功能上看:拦截器栈也是拦截器 默认拦截器栈: - 在struts-core.jar中 ...

  4. 5.Struts2中的拦截器

    拦截器是Struts2中的核心,其自带很多很多的拦截器,这里主要介绍一下自定义拦截器,恩多一半情况下呢?我们不需要使用到自定义的拦截器,Struts2本身已经提 供了很多的拦截器供我们使用,对于自定义 ...

  5. struts2基础——自定义拦截器

    一.自定义拦截器 默认的拦截器能实现的功能是有限的,Struts2 支持自定义拦截器. 二.拦截器类 1.实现 Interceptor 接口 2.继承 AbstractInterceptor 抽象类, ...

  6. Struts2(十四)拦截器实现权限管理

    一.认识拦截器 拦截器也是一个类 拦截器可以在Action被调用之前和之后执行代码 框架很多核心功能是拦截器实现的 拦截器的特点: 拦截器自由组合,增强了灵活性.扩展性.有利于系统解耦 拦截器可以拦截 ...

  7. 十五、struts2中的拦截器(框架功能核心)

    十五.struts2中的拦截器(框架功能核心) 1.过滤器VS拦截器 功能是一回事. 过滤器是Servlet规范中的技术,可以对请求和响应进行过滤. 拦截器是Struts2框架中的技术,实现AOP(面 ...

  8. Struts2透过自定义拦截器实现登录之后跳转到原页面

    Struts2通过自定义拦截器实现登录之后跳转到原页面 这个功能对用户体验来说是非常重要的.实现起来其实很简单. 拦截器的代码如下: package go.derek.advice; import g ...

  9. Struts2笔记_拦截器

    A.拦截器是什么 --- Interceptor:拦截器,起到拦截Action的作用. ---Filter:过滤器,过滤从客户端向服务器发送的请求. ---Interceptor:拦截器,拦截是客户端 ...

随机推荐

  1. MY97 日期控件只输入今天之前的值

    <script type="text/javascript">                        function GetDate() {          ...

  2. Codeforces Round #164 (Div. 2) A. Games【暴力/模拟/每个球队分主场和客场,所有球队两两之间进行一场比赛,要求双方球服颜色不能相同】

    A. Games time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  3. Sum of bit differences among all pairs

    This article was found from Geeksforgeeks.org. Click here to see the original article. Given an inte ...

  4. Spoj SUBST1 New Distinct Substrings

    Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...

  5. 如何避免CSS :before、:after 中文乱码

    问题: 在进行页面开发时,经常会使用:before, :after伪元素创建一些小tips,但是在:before或:after的content属性使用中文的话,会导致某些浏览器上出现乱码. 解决方案: ...

  6. Android中Context详解 ---- 你所不知道的Context(转)

    Android中Context详解 ---- 你所不知道的Context(转)                                               本文出处 :http://b ...

  7. [Bug] 未找到导入的项目“C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications\Microsoft.WebApplication.targets”

    This is very easy to do. Open your build definition and go to the "Process" page. Then und ...

  8. UBIFS - UBI File-System

    参考:http://www.linux-mtd.infradead.org/doc/ubifs.html#L_raw_vs_ftl UBIFS - UBI File-System Table of c ...

  9. Docker删除全部镜像和容器

    杀死所有正在运行的容器 docker kill $(docker ps -a -q) 删除所有已经停止的容器 docker rm $(docker ps -a -q) 删除所有未打 dangling ...

  10. SilverLight-3:目录

    ylbtech-SilverLight-Index: 1.A,返回顶部 Layout The Layout Containers - The Panel Background Borders   Si ...