© 版权声明:本文为博主原创文章,转载请注明出处
拦截器:
- 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.效果预览