配置struts.xml
<?xml version="1.0" encoding="gbk"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 设置Web应用的默认编码集为gbk -->
<constant name="struts.i18n.encoding" value="gbk" />
<!-- 设置Web应用的默认Locale为zh_CN -->
<constant name="struts.locale" value="zh_CN" />
<!-- 设置Struts2.1应用的国际化资源文件,多个文件中间可用逗号分隔 -->
<constant name="struts.custom.i18n.resources" value="MessageResource,globalMessage" />
<!-- 设置Struts2.1应用是否处于开发模式,通常在开发调试阶段设为true,正式上线后可设为false -->
<constant name="struts.devMode" value="true" />
<!-- 设置Struts2.1的默认主题为simple -->
<constant name="struts.ui.theme" value="simple" />
<!-- 设置上传文件临时文件夹 -->
<constant name="struts.multipart.saveDir" value="/tmp" />
<package name="michael" namespace="/michael" extends="struts-default">
<interceptors>
<interceptor name="Michael" class="com.intercepter.MichaelIntercepter" > <param name="name">micael</param><!-- 配置拦截器名字 -->
</interceptor>
</interceptors>
<action name="login" class="com.book.struts.action.LoginAction">
<result>/WEB-INF/jsp/success.jsp</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="Michael">
<param name="name">micahel拦截器</param>
</interceptor-ref>
</action>
</package>
</struts>
<!-----------------------------配置自定义拦截器------------------------------->
package com.intercepter;
import java.util.Date;
import com.book.struts.action.LoginAction;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class MichaelIntercepter implements Interceptor {
private String name;
public void destroy() {
}
public void init() {
}
public String intercept(ActionInvocation actionInvocation) throws Exception {
// 取得被拦截的Action
LoginAction la = (LoginAction)actionInvocation.getAction();
//设置Action的属性
la.setName("u0");
la.setAge("20");
System.out.println("Action执行之前"+name + "拦截器的动作"+new Date());
long startime = System.currentTimeMillis();
System.out.println("execute方法开始执行................");
// 如果该拦截器之后没有其他拦截器,在struts.xml 配置Action 中如果没有指定方法则执行execute()
//,如果有则执行指定的方法
String result = actionInvocation.invoke();
//action执行结束时间
System.out.println("Action执行之后"+name + "拦截器的动作"+new Date());
long endtime = System.currentTimeMillis();
System.out.println("执行Action需要"+(endtime-startime)+"毫秒");
return result;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<!--------------------------------------配置Action--------------------------------->
package com.book.struts.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String name;
private String age;
public String login() {
return SUCCESS;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
<!-----------------------------配置成功页面------------------------------------>
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
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 'regSuccess.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:property value="name"/><br/>
<s:property value="age"/>
</body>
</html>
<!-------------------------完成部署项目------------------------------->
访问:http://localhost:8080/Books/michael/login
如果没有出错,在后台会输出

测试一下拦截器执行的顺序
修改struts.xml 添加
<interceptor-ref name="Michael">
<param name="name">micahe2拦截器</param>
</interceptor-ref>
访问:http://localhost:8080/Books/michael/login
如果没有出错,在后台会输出

从图中可以看出拦截器的执行顺序为 :在Action的控制方法执行之前,位于拦截器链前面的拦截器将先起作用
,在action的控制方法执行之后,位于拦截器链前面的拦截器将后起作用。
自定义拦截器的实现,应该实现com.opensymphony.xwork2.interceptor.Interceptor接口,该接口有三个方法
1.void destory();//销毁该拦截器之前的回调方法
2.void init(); //初始化该拦截器的回调方法
3.String intercept(ActionInvocation invocation);//拦截器实现拦截的逻辑方法,如果该方法直接返回一个字符串,系统将会跳转到该逻辑视图对应的实际视图资源,不会调用被拦截的action。