struts2拦截器

interceptor:

自定义的拦截器必须要实现Interceptor接口

1:编写实现Interceptor接口的类

2:在struts.xml中文件中定义拦截器

3:在action中使用拦截器


package interceptor;

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

public class TheInterceptor1 implements Interceptor {

	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void init() {//类似于过滤器,服务器启动时就会实例化拦截器并调用init方法
		System.out.println("init invoked!");
	}

	public String intercept(ActionInvocation invocation) throws Exception {
		System.out.println("before");
		String result = invocation.invoke();
		System.out.println("after");
		return result;
	}

}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
	<package name = "sss" extends="struts-default" namespace="/">
			<interceptors>
				<interceptor name = "theInterceptor1" class = "interceptor.TheInterceptor1"></interceptor>
			</interceptors>
			<global-results>
				<result name="usernameInvalid" >/usernameInvalid.jsp</result>
				<result name = "passwordInvalid">/passwordInvalid.jsp</result>
			</global-results>
			<global-exception-mappings>
				<exception-mapping result="usernameInvalid" exception="exception.UsernameException"></exception-mapping>
				<exception-mapping result="passwordInvalid" exception="exception.PasswordException"></exception-mapping>
			</global-exception-mappings>
			<action name="UserAction2" class="action.UserAction2">
			</action>
			<action name = "login" class="action.LoginAction">
				<result name = "success" type = "redirect">/result.jsp</result>
				<result name = "input">/login.jsp</result>
			</action>
			<action name = "register" class = "action.RegisterAction">
				<result name = "success">/registerResult.jsp</result>
				<result name = "input">/regist.jsp</result>
			</action>
			<action name = "action1" class = "action.Action1">
				<result name = "success" type = "chain">
					<param name="actionName">action2</param>
					<param name="username">sdljfsljfsdjfk</param>
					<param name="password">${password}</param>
					<param name="a">demo</param>
					<param name="usernameAndpassword">${usernameAndpassword}</param>
				</result>
				<interceptor-ref name="theInterceptor1"></interceptor-ref>
         <interceptor-ref name = "defaultStack"></interceptor-ref>
	</action>
	<action name = "action2" class = "action.Action2">
        <result name = "success">/action2.jsp</result>
        </action>
        <action name = "token" class = "action.TokenAction">
        <result name = "success" type = "redirect">/tokenSuccess.jsp</result>
        <!-- invalid.token 是struts2 提供的 -->
        <result name = "invalid.token" >/tokenFail.jsp</result>
       <interceptor-ref name = "token"></interceptor-ref>
       <interceptor-ref name = "defaultStack"></interceptor-ref>
       </action>
       </package>
        </struts>
package interceptor;

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

public class TheInterceptor1 implements Interceptor {
	private String test;
	
	public String getTest() {
		return test;
	}

	public void setTest(String test) {
		this.test = test;
	}

	public void destroy() {
		
	}

	public void init() {
		System.out.println("testvalue:"+test);
		System.out.println("init invoked!");
	}

	public String intercept(ActionInvocation invocation) throws Exception {
		System.out.println("before");
		String result = invocation.invoke();
		System.out.println("after");
		return result;
	}

}

定义拦截器是可以直接继承AbstractInterceptor抽象类(该类实现了Interceptor)接口,

并且对init和destroy方法进行了空实现),然后实现其抽象方法intercept即可。

方法拦截器(可以对指定的方法进行拦截)

package interceptor;

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

public class TheInterceptor3 extends MethodFilterInterceptor {

	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		System.out.println("TheInterceptor3 before");
		String name = invocation.invoke();
		System.out.println("TheInterceptor3 after");
		return name;
	}

}
继承MethodFilterInterceptor

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
	<package name = "sss" extends="struts-default" namespace="/">
			<interceptors>
				<interceptor name = "theInterceptor1" class = "interceptor.TheInterceptor1">
					<param name="test">zhangsan</param>
				</interceptor>
				<interceptor name = "theInterceptor2" class = "interceptor.TheInterceptor2"></interceptor>
				<interceptor name = "theInterceptor3" class = "interceptor.TheInterceptor3"></interceptor>
			</interceptors>
			<global-results>
				<result name="usernameInvalid" >/usernameInvalid.jsp</result>
				<result name = "passwordInvalid">/passwordInvalid.jsp</result>
			</global-results>
			<global-exception-mappings>
				<exception-mapping result="usernameInvalid" exception="exception.UsernameException"></exception-mapping>
				<exception-mapping result="passwordInvalid" exception="exception.PasswordException"></exception-mapping>
			</global-exception-mappings>
			<action name="UserAction2" class="action.UserAction2">
			</action>
			<action name = "login" class="action.LoginAction">
				<result name = "success" type = "redirect">/result.jsp</result>
				<result name = "input">/login.jsp</result>
			</action>
			<action name = "register" class = "action.RegisterAction">
				<result name = "success">/registerResult.jsp</result>
				<result name = "input">/regist.jsp</result>
			</action>
			<action name = "action1" class = "action.Action1" method = "my">
				<result name = "success" type = "chain">
					<param name="actionName">action2</param>
					<param name="username">sdljfsljfsdjfk</param>
					<param name="password">${password}</param>
					<param name="a">demo</param>
					<param name="usernameAndpassword">${usernameAndpassword}</param>
				</result>
				
			
				<interceptor-ref name="theInterceptor1"></interceptor-ref>
				<interceptor-ref name = "theInterceptor2"></interceptor-ref>
					<interceptor-ref name = "theInterceptor3">
					<param name="</spanincludeMethods">mysdf,execute</param>//拦截mysdf方法和execute方法
				</interceptor-ref>
				<interceptor-ref name="defaultStack"></interceptor-ref>
			</action>
			<action name = "action2" class = "action.Action2">
				<result name = "success">/action2.jsp</result>
			</action>
			<action name = "token" class = "action.TokenAction">
				<result name = "success" type = "redirect">/tokenSuccess.jsp</result>
				<!-- invalid.token 是struts2 提供的 -->
				<result name = "invalid.token" >/tokenFail.jsp</result>
				<interceptor-ref name = "token"></interceptor-ref>
				<interceptor-ref name = "defaultStack"></interceptor-ref>
			</action>
	</package>
</struts>

在方法过滤器中,如果即没有制定includeMethods参数,也没有制定execludeMethods

参数,那么所有的方法都会被被拦截,也就是说所有的方法都被认为是

includeMethods的。

如果制定了includeMethods,那么只会拦截includeMethods中的方法,没有

包含在includeMethods中的方法就不会被拦截。


 一旦定义了自己的拦截器,将其配置到action上后,需要在action的最后加上默认的拦截器栈:defaultStack。
                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值