struts2拦截器剖析

一.Struts2使用拦截器步骤:
1. 定义拦截器类
2. 声明拦截器
3. 使用拦截器
1. 定义拦截器MyInterceptor.java

package com.test.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class MyInterceptor implements Interceptor {

@Override
public void destroy() {
System.out.println("destroy invoked");
}

@Override
public void init() {
System.out.println("init invoked");
}

@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("intercept invoked");
String result=invocation.invoke();
System.out.println("intercept invoke result:"+result);
return result;
}

}

2. 声明拦截器 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="struts2" extends="struts-default">

<interceptors>
<interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"></interceptor>
</interceptors>

<action name="helloworld" class="com.test.action.HelloWorld">
<result>/helloworld.jsp</result>
</action>

<action name="login" class="com.test.action.LoginAction">
<result>/result.jsp</result>
<result name="input">/login2.jsp</result>
</action>

<action name="converterAction" class="com.test.action.PointAction" method="test" >
<result name="success">/output.jsp</result>
<result name="input">/error.jsp</result>
</action>

<action name="register" class="com.test.action.RegisterAction">
<result name="success">/success.jsp</result>
<result name="input">/register.jsp</result>
</action>

</package>
</struts>

3. 使用拦截器 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="struts2" extends="struts-default">

<interceptors>
<interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"></interceptor>
</interceptors>

<action name="helloworld" class="com.test.action.HelloWorld">
<result>/helloworld.jsp</result>
</action>

<action name="login" class="com.test.action.LoginAction">
<result>/result.jsp</result>
<result name="input">/login2.jsp</result>
</action>

<action name="converterAction" class="com.test.action.PointAction" method="test" >
<result name="success">/output.jsp</result>
<result name="input">/error.jsp</result>
</action>

<action name="register" class="com.test.action.RegisterAction">
<result name="success">/success.jsp</result>
<result name="input">/register.jsp</result>

<interceptor-ref name="myInterceptor"></interceptor-ref>

</action>

</package>
</struts>

注:拦截器在服务器启动时就加载,并初始化。
二.Struts2.xml中使用拦截器问题
1. 如上使用拦截器会发现输入结果不能被传到结果页面
这是因为strut2.xml继承了struts-default文件,这个XML文件中定义了很多拦截器,且定义了默认使用的拦截器组defaultStack,如果在struts.xml中Aciton使用了自定的拦截器,默认的使用的拦截器无效,解决方法就是在引用一下defaultStack拦截器

<action name="register" class="com.test.action.RegisterAction">
<result name="success">/success.jsp</result>
<result name="input">/register.jsp</result>

<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>

三.Struts2.xml中使用拦截器同Filter可以对黑名单进行操作
1. 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="struts2" extends="struts-default">

<interceptors>
<interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor">
<param name="hello">world</param>
</interceptor>
</interceptors>

<action name="helloworld" class="com.test.action.HelloWorld">
<result>/helloworld.jsp</result>
</action>

<action name="login" class="com.test.action.LoginAction">
<result>/result.jsp</result>
<result name="input">/login2.jsp</result>
</action>

<action name="converterAction" class="com.test.action.PointAction" method="test" >
<result name="success">/output.jsp</result>
<result name="input">/error.jsp</result>
</action>

<action name="register" class="com.test.action.RegisterAction">
<result name="success">/success.jsp</result>
<result name="input">/register.jsp</result>

<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>

</package>
</struts>

2. MyInterceptor.java

package com.test.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class MyInterceptor implements Interceptor {

private String hello;

public String getHello() {
return hello;
}

public void setHello(String hello) {
this.hello = hello;
}

@Override
public void destroy() {
System.out.println("destroy invoked");
}

@Override
public void init() {
System.out.println("init invoked");
System.out.println("hello:"+hello);
}

@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("intercept invoked");
String result=invocation.invoke();
System.out.println("intercept invoke result:"+result);
return result;
}

}

四.为了避免实现Interceptor接口时要实现init(),destory(),只需要interceptor(),可以继承抽象类AbstractInterceptor
1. AbstractInterceptor.java

package com.test.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class MyInterceptor2 extends AbstractInterceptor {

@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.err.println("MyInterceptor2 invoked");
String result=invocation.invoke();
System.out.println("MyInterceptor2 invoke result:"+result);
return result;
}

}

2. 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="struts2" extends="struts-default">

<interceptors>
<interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor">
<param name="hello">world</param>
</interceptor>
<interceptor name="myInterceptor2" class="com.test.interceptor.MyInterceptor2">
</interceptor>
</interceptors>

<action name="helloworld" class="com.test.action.HelloWorld">
<result>/helloworld.jsp</result>
</action>

<action name="login" class="com.test.action.LoginAction">
<result>/result.jsp</result>
<result name="input">/login2.jsp</result>
</action>

<action name="converterAction" class="com.test.action.PointAction" method="test" >
<result name="success">/output.jsp</result>
<result name="input">/error.jsp</result>
</action>

<action name="register" class="com.test.action.RegisterAction">
<result name="success">/success.jsp</result>
<result name="input">/register.jsp</result>

<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="myInterceptor2"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>

</package>
</struts>

五.Action中的方法拦截器,继承抽象类MethodFilterInterceptor
1. MethodFilterInterceptor.java

package com.test.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class MyInterceptor3 extends MethodFilterInterceptor {

@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
System.out.println("MyInterceptor3 invoked");
String result=invocation.invoke();
System.out.println("MyInterceptor3 invoke result:"+result);
return result;
}

}

2. 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="struts2" extends="struts-default">

<interceptors>
<interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor">
<param name="hello">world</param>
</interceptor>
<interceptor name="myInterceptor2" class="com.test.interceptor.MyInterceptor2">
</interceptor>
<interceptor name="myInterceptor3" class="com.test.interceptor.MyInterceptor3">
</interceptor>
</interceptors>

<action name="helloworld" class="com.test.action.HelloWorld">
<result>/helloworld.jsp</result>
</action>

<action name="login" class="com.test.action.LoginAction">
<result>/result.jsp</result>
<result name="input">/login2.jsp</result>
</action>

<action name="converterAction" class="com.test.action.PointAction" method="test" >
<result name="success">/output.jsp</result>
<result name="input">/error.jsp</result>
</action>

<action name="register" class="com.test.action.RegisterAction">
<result name="success">/success.jsp</result>
<result name="input">/register.jsp</result>

<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="myInterceptor2"></interceptor-ref>
<interceptor-ref name="myInterceptor3">
<!-- <param name="excludeMethods">execute</param> -->
没有param就拦截所有方法,includeMethods拦截指定方法,excludeMethods不拦截指定方法
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>

</package>
</struts>

六.拦截器栈(拦截器的集合)
1. 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="struts2" extends="struts-default">

<interceptors>
<interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor">
<param name="hello">world</param>
</interceptor>
<interceptor name="myInterceptor2" class="com.test.interceptor.MyInterceptor2">
</interceptor>
<interceptor name="myInterceptor3" class="com.test.interceptor.MyInterceptor3">
</interceptor>

<interceptor-stack name="myStack">
<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="myInterceptor2"></interceptor-ref>
</interceptor-stack>
</interceptors>

<action name="helloworld" class="com.test.action.HelloWorld">
<result>/helloworld.jsp</result>
</action>

<action name="login" class="com.test.action.LoginAction">
<result>/result.jsp</result>
<result name="input">/login2.jsp</result>
</action>

<action name="converterAction" class="com.test.action.PointAction" method="test" >
<result name="success">/output.jsp</result>
<result name="input">/error.jsp</result>
</action>

<action name="register" class="com.test.action.RegisterAction">
<result name="success">/success.jsp</result>
<result name="input">/register.jsp</result>
<!--
<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="myInterceptor2"></interceptor-ref>
-->

<interceptor-ref name="myStack"></interceptor-ref>

<interceptor-ref name="defaultStack"></interceptor-ref>
</action>

</package>
</struts>

七.自定义默认的拦截器
1. 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="struts2" extends="struts-default">

<interceptors>
<interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor">
<param name="hello">world</param>
</interceptor>
<interceptor name="myInterceptor2" class="com.test.interceptor.MyInterceptor2">
</interceptor>
<interceptor name="myInterceptor3" class="com.test.interceptor.MyInterceptor3">
</interceptor>

<interceptor-stack name="myStack">
<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="myInterceptor2"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>

<default-interceptor-ref name="myStack"></default-interceptor-ref>

<action name="helloworld" class="com.test.action.HelloWorld">
<result>/helloworld.jsp</result>
</action>

<action name="login" class="com.test.action.LoginAction">
<result>/result.jsp</result>
<result name="input">/login2.jsp</result>
</action>

<action name="converterAction" class="com.test.action.PointAction" method="test" >
<result name="success">/output.jsp</result>
<result name="input">/error.jsp</result>
</action>

<action name="register" class="com.test.action.RegisterAction">
<result name="success">/success.jsp</result>
<result name="input">/register.jsp</result>
<!--
<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="myInterceptor2"></interceptor-ref>
<interceptor-ref name="myStack"></interceptor-ref>
-->

</action>

</package>
</struts>

注:自定义默认的拦截器会对所有Action生效,所有一般情况不会用自定默认拦截器
八.在拦截器中增加监听器(观察者模式)
1. 定义监听器MyListener.java实现PreResultListener(在execute方法执行完,result执行之前调用)接口

package com.test.listener;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.PreResultListener;

public class MyListener implements PreResultListener {

@Override
public void beforeResult(ActionInvocation invocation, String resultCode) {
System.out.println("MyListener invoked");
}

}

2. 在拦截器MyInterceptor中增加监听器

package com.test.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.test.listener.MyListener;

public class MyInterceptor implements Interceptor {

private String hello;

public String getHello() {
return hello;
}

public void setHello(String hello) {
this.hello = hello;
}

@Override
public void destroy() {
System.out.println("destroy invoked");
}

@Override
public void init() {
System.out.println("init invoked");
System.out.println("hello:"+hello);
}

@Override
public String intercept(ActionInvocation invocation) throws Exception {

invocation.addPreResultListener(new MyListener());

System.out.println("intercept invoked");
String result=invocation.invoke();
System.out.println("intercept invoke result:"+result);
return result;
}

}

对于以上方式,只有一处会用到的一个类的一个简单方法,可以不用自定义监听器,直接在拦截器中用一个匿名类来实现,如下:

@Override
public String intercept(ActionInvocation invocation) throws Exception {

invocation.addPreResultListener(new PreResultListener(){
@Override
public void beforeResult(ActionInvocation invocation,
String resultCode) {
System.out.println("hello world");
}
});

System.out.println("intercept invoked");
String result=invocation.invoke();
System.out.println("intercept invoke result:"+result);
return result;
}

九.拦截器应用的场合(最重要的应用是权限验证)
1. 定义权限拦截器AuthInterceptor.java

package com.test.interceptor;

import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class AuthInterceptor extends AbstractInterceptor {

@SuppressWarnings("unchecked")
@Override
public String intercept(ActionInvocation invocation) throws Exception {
Map map=invocation.getInvocationContext().getSession();
if(null==map.get("user")){ //用户没有登录
return Action.LOGIN;
}else{
return invocation.invoke(); //转到下一个拦截器
}
}

}

2. 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="struts2" extends="struts-default">

<interceptors>
<interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor">
<param name="hello">world</param>
</interceptor>
<interceptor name="myInterceptor2" class="com.test.interceptor.MyInterceptor2">
</interceptor>
<interceptor name="myInterceptor3" class="com.test.interceptor.MyInterceptor3">
</interceptor>
//声明权限拦截器
<interceptor name="authInterceptor" class="com.test.interceptor.AuthInterceptor">
</interceptor>
//使用权限拦截器
<interceptor-stack name="myStack">
<interceptor-ref name="authInterceptor"></interceptor-ref>
<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="myInterceptor2"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
//使用全局结果集,对所有Action生效
<global-results>
//type的值,可以在struts-default.xml中查找,redirect是重定向,默认是dispatcher转发
<result name="login" type="redirect">/login.jsp</result>
</global-results>
<!--
<default-interceptor-ref name="myStack"></default-interceptor-ref>
-->
<action name="helloworld" class="com.test.action.HelloWorld">
<result>/helloworld.jsp</result>
</action>

<action name="login" class="com.test.action.LoginAction">
<result>/result.jsp</result>
<result name="input">/login2.jsp</result>
</action>

<action name="converterAction" class="com.test.action.PointAction" method="test" >
<result name="success">/output.jsp</result>
<result name="input">/error.jsp</result>
</action>

<action name="register" class="com.test.action.RegisterAction">
<result name="success">/success.jsp</result>
<result name="input">/register.jsp</result>
<!--
<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="myInterceptor2"></interceptor-ref>
-->
<interceptor-ref name="myStack"></interceptor-ref>


</action>

</package>
</struts>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值