拦截器:它是struts2的核心,它介于请求与Action之间,它帮助我们实现了模块化程序设计以及“插拔式“程序设计。
要书写一个拦截器类,要实现Interceptor接口:
一,实现Interceptor接口,实现他的三个方法:destroy;init;intercept(ActionInvocation argo)。
拦截器1:
package com.buaa.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class Inte1 implements Interceptor {
public void destroy() {
System.out.println("........destroy1()..........");
}
public void init() {
System.out.println("...........init1().............");
}
public String intercept(ActionInvocation arg0) throws Exception {
System.out.println("方法执行之前拦截1");
String s = arg0.invoke();
System.out.println("方法执行之后拦截1");
return s;
}
}
拦截器2:
package com.buaa.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class Inte2 implements Interceptor {
public void destroy() {
System.out.println("........destroy2()..........");
}
public void init() {
System.out.println("...........init2().............");
}
public String intercept(ActionInvocation arg0) throws Exception {
System.out.println("方法执行之前拦截2");
String s = arg0.invoke();
System.out.println("方法执行之后拦截2");
return s;
}
}
拦截器3:
package com.buaa.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class Inte3 implements Interceptor {
public void destroy() {
System.out.println("........destroy3()..........");
}
public void init() {
System.out.println("...........init3().............");
}
public String intercept(ActionInvocation arg0) throws Exception {
System.out.println("方法执行之前拦截3");
String s = arg0.invoke();
System.out.println("方法执行之后拦截3");
return s;
}
}
二,创建action类
package com.buaa.action;
import com.opensymphony.xwork2.ActionSupport;
public class ShowAction extends ActionSupport{
@Override
public String execute() throws Exception {
System.out.println("..........show()..........");
return "show";
}
}
三,在struts.xml中,声明拦截器,且若要使用它,必须在相应的action中配置相应的拦截器。
拦截器栈:多个拦截器或拦截器栈的集合
Struts2中提供了很多种拦截器,可以实现很多功能 ,例如:上传下载,模型驱动,国际化。。。。
但是,一旦在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>
<package name="default" namespace="/" extends="struts-default">
<!-- 声明拦截器 -->
<interceptors>
<interceptor name="inte1" class="com.buaa.interceptor.Inte1"></interceptor>
<interceptor name="inte2" class="com.buaa.interceptor.Inte2"></interceptor>
<interceptor name="inte3" class="com.buaa.interceptor.Inte3"></interceptor>
<interceptor-stack name="sta1">
<interceptor-ref name="inte1"></interceptor-ref>
<interceptor-ref name="inte2"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="show" class="com.buaa.action.ShowAction">
<!-- 拦截器的配置 -->
<interceptor-ref name="sta1"></interceptor-ref>
<interceptor-ref name="inte3"></interceptor-ref>
<result name="show">/show.jsp</result>
</action>
</package>
</struts>
四、演示结果:
页面展示:
项目布局: