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中的方法就不会被拦截。