struts2拦截器的使用方式

一.struts2拦截器的使用方式1(实现Interceptor接口)
1.MyInterceptor.java(带参数的拦截器的定义)
package com.hitsoft.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.interceptor.PreResultListener;

@SuppressWarnings("serial")
public class MyInterceptor implements Interceptor{
private String hello;

public String getHello() {
return hello;
}

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

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

public void init() {
System.out.println("init invoked!");
System.out.println("hello=" + hello);
}

public String intercept(ActionInvocation invocation) throws Exception {
invocation.addPreResultListener(new PreResultListener(){

public void beforeResult(ActionInvocation invocation,
String resultCode) {
System.out.println("innerClass PreResultListener invoked!");

}});
System.out.println("MyInterceptor1 invoked!");
String result= invocation.invoke();
System.out.println("hello1=" + hello);
System.out.println("result1="+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.hitsoft.interceptor.MyInterceptor">
<param name="hello">world</param>
</interceptor>
</interceptors>
<action name="login" class="com.hitsoft.action.LoginAction">
<result name="success">/result.jsp</result>
<result name="input">/login.jsp</result>
<interceptor-ref name="myInterceptor"/>
</action>
</package>
</struts>

二.struts2拦截器的使用方式2(继承AbstractInterceptor抽象类)
1.MyInterceptor2.java(带参数的拦截器的定义)

package com.hitsoft.interceptor;

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

@SuppressWarnings("serial")
public class MyInterceptor2 extends AbstractInterceptor{
private String hello;

public String getHello() {
return hello;
}

public void setHello(String hello) {
this.hello = hello;
}
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("MyInterceptor2 invoked!");
String result= invocation.invoke();
System.out.println("hello2=" + hello);
System.out.println("result2="+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="myInterceptor2" class="com.hitsoft.interceptor.MyInterceptor2">
<param name="hello">world</param>
</interceptor>
</interceptors>
<action name="login" class="com.hitsoft.action.LoginAction">
<result name="success">/result.jsp</result>
<result name="input">/login.jsp</result>
<interceptor-ref name="myInterceptor2"/>
</action>
</package>
</struts>


三.struts2拦截器的使用方式3(继承MethodFilterInterceptor抽象类)
1.MyInterceptor3.java(带参数的拦截器的定义)

package com.hitsoft.interceptor;

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

@SuppressWarnings("serial")
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("result3="+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="myInterceptor3" class="com.hitsoft.interceptor.MyInterceptor3">
<param name="hello">world</param>
</interceptor>
</interceptors>
<action name="login" class="com.hitsoft.action.LoginAction">
<result name="success">/result.jsp</result>
<result name="input">/login.jsp</result>
<interceptor-ref name="myInterceptor3"/>
</action>
</package>
</struts>


四.struts2拦截器的使用方式4(默认定义拦截器)
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.hitsoft.interceptor.MyInterceptor">
<param name="hello">world</param>
</interceptor>
<!-- 第二种拦截器 -->
<interceptor name="myInterceptor2" class="com.hitsoft.interceptor.MyInterceptor2">
<param name="hello">world</param>
</interceptor>
<!-- 第三种拦截器 -->
<interceptor name="myInterceptor3" class="com.hitsoft.interceptor.MyInterceptor3">
</interceptor>
<!-- 第四种拦截器栈 -->
<interceptor-stack name="myStack">
<interceptor-ref name="myInterceptor" ></interceptor-ref>
<interceptor-ref name="myInterceptor2"></interceptor-ref>
<interceptor-ref name="myInterceptor3"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 默认定义拦截器栈 -->
<default-interceptor-ref name="myStack"></default-interceptor-ref>
<action name="login" class="com.hitsoft.action.LoginAction">
<result name="success">/result.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>

5.自定义并配置所有拦截器到action配置中:
<?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.hitsoft.interceptor.MyInterceptor">
<param name="hello">world</param>
</interceptor>
<!-- 第二种拦截器 -->
<interceptor name="myInterceptor2" class="com.hitsoft.interceptor.MyInterceptor2">
<param name="hello">world</param>
</interceptor>
<!-- 第三种拦截器 -->
<interceptor name="myInterceptor3" class="com.hitsoft.interceptor.MyInterceptor3">
</interceptor>
</interceptors>
<action name="login" class="com.hitsoft.action.LoginAction">
<result name="success">/result.jsp</result>
<result name="input">/login.jsp</result>
<interceptor-ref name="myInterceptor" ></interceptor-ref>
<interceptor-ref name="myInterceptor2"></interceptor-ref>
<interceptor-ref name="myInterceptor3">
<param name="excludeMethods">execute</param>
<param name="includeMethods">execute</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值