拦截器(Interceptor )是Struts 2 的核心组成部分。很多功能(Feature )都是构建在拦截器基础之上的,例如文件的上传和下载、国际化、转换器和数据校验等,Struts 2 利用内建的拦截器,完成了框架内的大部分操作。
在Struts 2 文档中对拦截器的解释为——拦截器是动态拦截Action 调用的对象。它提供了一种机制,使开发者可以定义一个特定的功能模块,这个模块可以在Action 执行之前或者之后运行,也可以在一个Action 执行之前阻止Action 执行。同时也提供了一种可以提取Action 中可重用的部分的方式
拦截器在Struts2中的示意图:
![](https://p-blog.csdn.net/images/p_blog_csdn_net/Struts2BBS/5-4.jpg)
从上图可以看出,Struts 2 架构的 Action 被一个或者多个拦截器(拦截器栈)所包围,所有的用户请求都会被拦截器所拦截,然后交给 Action 处理,处理结果以逻辑视图方式返回给用户。而这个调用执行流程,是由 Struts 2 的配置文件来实现的,后面会详细介绍。拦截器是 Struts 2 核心部分之一。
当用户请求到达 Struts 2 的 ServletDispatcher 时, Struts 2 会查找配置文件,并根据其配置实例化相对的拦截器对象,然后串成一个列表( List ),最后一个一个地调用列表中的拦截器
拦截器时序图如下图所示:
![](https://p-blog.csdn.net/images/p_blog_csdn_net/Struts2BBS/5-5.jpg)
在 Struts 2 架构中, Action 的调用都是通过拦截器来实现的。有的读者可能会疑惑,为什么没有明确说明拦截器,为什么可以直接调用 Action ?那是因为
Struts 2 架构如果不做显式的拦截器配置,则系统会调用默认的拦截器来调用 Action ,在用户看来,好像没有配置拦截器 。
Hello World拦截器示例.
程序要演示的是显示地让拦截器调用Action,来体会拦截器的用途。
一般情况下我们都会先写出一个Action,然后配置struts.xml文件
MyAction.java
public class MyAction extends ActionSupport {
//以下属性信息都是从前台(JSP页面获得)
private String username;
private String mymsg;
private String password1;
private String password2;
private Date birthday;
public String execute(){
if (username!=null &&this .getPassword1().equals(this .getPassword2())&&!this.getUsername().trim().equals("")){
//输出调试信息
System.out.println("Action信息,正在执行Action.... " );
return SUCCESS;
}else {
return INPUT;
}
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this .username = username;
}
public String getMymsg() {
return mymsg;
}
public void setMymsg(String mymsg) {
this .mymsg = mymsg;
}
public String getPassword1() {
return password1;
}
public void setPassword1(String password1) {
this .password1 = password1;
}
public String getPassword2() {
return password2;
}
public void setPassword2(String password2) {
this .password2 = password2;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this .birthday = birthday;
}
}
//以下属性信息都是从前台(JSP页面获得)
private String username;
private String mymsg;
private String password1;
private String password2;
private Date birthday;
public String execute(){
if (username!=null &&this .getPassword1().equals(this .getPassword2())&&!this.getUsername().trim().equals("")){
//输出调试信息
System.out.println("Action信息,正在执行Action.... " );
return SUCCESS;
}else {
return INPUT;
}
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this .username = username;
}
public String getMymsg() {
return mymsg;
}
public void setMymsg(String mymsg) {
this .mymsg = mymsg;
}
public String getPassword1() {
return password1;
}
public void setPassword1(String password1) {
this .password1 = password1;
}
public String getPassword2() {
return password2;
}
public void setPassword2(String password2) {
this .password2 = password2;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this .birthday = birthday;
}
}
自定义拦截器
MyInterceptor.java
public class MyInterceptor extends AbstractInterceptor {
//拦截方法
public String intercept(ActionInvocation invocation) throws Exception {
MyAction myA=(MyAction) invocation.getAction();
System.out.println("拦截器信息:hello world 拦截器" );
//执行action或者下一个拦截器
String result=invocation.invoke();
System.out.println("拦截器信息:Action执行完毕" );
return result;
}
}
//拦截方法
public String intercept(ActionInvocation invocation) throws Exception {
MyAction myA=(MyAction) invocation.getAction();
System.out.println("拦截器信息:hello world 拦截器" );
//执行action或者下一个拦截器
String result=invocation.invoke();
System.out.println("拦截器信息:Action执行完毕" );
return result;
}
}
下面我们开始配置struts.xml文件
首先我定义了一个my.xml文件
<? xml version ="1.0" encoding ="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
< struts >
< package name ="mynew" namespace ="/" extends ="struts-default" >
< interceptors >
< interceptor name ="myInterceptor" class ="com.MyInterceptor" >
</ interceptor >
</ interceptors >
< action name ="myAction" class ="com.MyAction" >
< result name ="success" > /success.jsp</ result >
< result name ="input" > /index.jsp</ result >
<!-- 引用默认拦截器 -->
< interceptor-ref name ="defaultStack" > </ interceptor-ref >
<!-- 引用自定义拦截器 -->
< interceptor-ref name ="myInterceptor" > </ interceptor-ref >
</ action >
</ package >
</ struts >
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
< struts >
< package name ="mynew" namespace ="/" extends ="struts-default" >
< interceptors >
< interceptor name ="myInterceptor" class ="com.MyInterceptor" >
</ interceptor >
</ interceptors >
< action name ="myAction" class ="com.MyAction" >
< result name ="success" > /success.jsp</ result >
< result name ="input" > /index.jsp</ result >
<!-- 引用默认拦截器 -->
< interceptor-ref name ="defaultStack" > </ interceptor-ref >
<!-- 引用自定义拦截器 -->
< interceptor-ref name ="myInterceptor" > </ interceptor-ref >
</ action >
</ package >
</ struts >
这样的话就要在struts.xml引入,
<? xml version ="1.0" encoding ="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
< struts >
< constant name ="struts.devMode" value ="true" />
< include file ="my.xml" > </ include >
</ struts >
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
< struts >
< constant name ="struts.devMode" value ="true" />
< include file ="my.xml" > </ include >
</ struts >
如果读者不习惯的话完全可以写在struts.xml文件中
index.jsp
< %@ page language ="java" import ="java.util.*" pageEncoding ="UTF-8" %>
< %@ taglib prefix ="s" uri ="/struts-tags" %>
< html >
< head >
</ head >
< body >
< s:form method ="post" action ="myAction" >
< s:textfield name ="username" label ="用户名" > </ s:textfield >
< s:password name ="password1" label ="密码" > </ s:password >
< s:password name ="password2" label ="确认密码" > </ s:password >
< s:submit value ="注册" > </ s:submit >
</ s:form >
</ body >
</ html >
< %@ taglib prefix ="s" uri ="/struts-tags" %>
< html >
< head >
</ head >
< body >
< s:form method ="post" action ="myAction" >
< s:textfield name ="username" label ="用户名" > </ s:textfield >
< s:password name ="password1" label ="密码" > </ s:password >
< s:password name ="password2" label ="确认密码" > </ s:password >
< s:submit value ="注册" > </ s:submit >
</ s:form >
</ body >
</ html >
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>Insert title here</title>
</head>
<body>
<h3>注册成功</h3>
用户名:<s:property value="username" /><p>
密码: <s:property value="password1" />
</body>
</html>
pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>Insert title here</title>
</head>
<body>
<h3>注册成功</h3>
用户名:<s:property value="username" /><p>
密码: <s:property value="password1" />
</body>
</html>
后台输出结果:
拦截器信息:hello world 拦截器
Action信息,正在执行Action....
2010-11-1 20:39:11 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find property [org.apache.catalina.jsp_file]
拦截器信息:Action执行完毕
Action信息,正在执行Action....
2010-11-1 20:39:11 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find property [org.apache.catalina.jsp_file]
拦截器信息:Action执行完毕