<interceptors>
用来定义拦截器,所有的拦截器和拦截器栈都是在此元素中定义的,可以包含子元素<interceptor>,<interceptor-stack><Interceptor>
用来定义拦截器,需要指定两个属性,name属性指定了拦截器的名字,class指定了拦截器的实现的类。这个是在<interceptors>下定义的<interceptor-stack>
用来定义拦截器栈,其中的name属性指定了拦截器栈的名称。另外在此元素下可以指定<interceptor-ref>引入其他的拦截器或者拦截器栈<interceptor-ref>
用来引用其他的拦截器或者拦截器栈,name属性指定了拦截器或者拦截栈的名称<param>
用来为拦截器指定参数,可以作为<interceptor>或者<interceptor-ref>的子元素。并且可以定义多个。其中的name属性指定了参数的名称<default-interceptor-ref>
将某一个拦截器定义为默认拦截器void init()
初始化拦截器执行的方法String intercept(ActionInvocation invocation) throws Exception
实现拦截器逻辑的主要方法。
ActionInvocation
包含了Action的引用,因此使用这个对象可以对Action进行相应的操作,比如可以获取和设置Action类的成员变量ActionInvocation
包含了Action的引用,可以调用其中的invoke()
方法继续调用下一个拦截器,如果后面没有拦截器了,那么就会执行Action中对应的映射方法,如果有,那么就会继续执行下一个拦截器,直到执行完全部的拦截器才会执行对应的映射方法void destroy()
销毁拦截器开启的资源import com.jsnu.struts2.controller.SimpleAction;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class TestInterceptor implements Interceptor{
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void init() {
// TODO Auto-generated method stub
}
@Override
public String intercept(ActionInvocation invocation) throws Exception {
Object object=invocation.getAction(); //获取当前调用拦截器的Action类的对象
//如果不为null,就可以获取其中的属性,否则空指针
if (object!=null) {
SimpleAction simpleAction=(SimpleAction)object;
//将User属性全部转换成大写字母
simpleAction.getUser().setName(simpleAction.getUser().getName().toUpperCase());
simpleAction.getUser().setPassword(simpleAction.getUser().getPassword().toUpperCase());
String result=invocation.invoke(); //调用下一个拦截器
System.out.println("成功跳转视图后执行的逻辑");
return result;
}
return null;
}
}
AbstractInterceptor
抽象类,里面只有一个抽象方法String intercept(ActionInvocation invocation)
,只需要实现这个方法即可,如果你需要初始化和销毁,那么也可以覆盖其中的init()和destroy()方法import com.jsnu.struts2.controller.SimpleAction;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
//继承类AbstractInterceptor
public class AbstractInterceptorTest extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
Object object = invocation.getAction(); // 获取当前调用拦截器的Action类的对象
// 如果不为null,就可以获取其中的属性,否则空指针
if (object != null) {
SimpleAction simpleAction = (SimpleAction) object;
// 将User属性全部转换成大写字母
simpleAction.getUser().setName(
simpleAction.getUser().getName().toUpperCase());
simpleAction.getUser().setPassword(
simpleAction.getUser().getPassword().toUpperCase());
String result = invocation.invoke(); // 调用下一个拦截器
System.out.println("成功跳转视图后执行的逻辑");
return result;
}
return null;
}
}
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref >
引用已经定义好的拦截器即可<package name="test" extends="struts-default" namespace="/">
<!-- 定义拦截器 -->
<interceptors>
<!-- 实现接口的拦截器 -->
<interceptor name="testInterceptor" class="com.jsnu.struts2.Interceptor.TestInterceptor"></interceptor>
<!-- 继承类的 -->
<interceptor name="abstractInterceptorTest" class="com.jsnu.struts2.Interceptor.AbstractInterceptorTest"></interceptor>
</interceptors>
<action name="simple" class="com.jsnu.struts2.controller.SimpleAction">
<result name="success">/jsp/success.jsp</result>
<result name="input">/jsp/input.jsp</result>
<!-- 在使用了自定的拦截器之后,那么系统默认的拦截器栈将会失去作用,因此这里需要重新指定拦截器栈-->
<interceptor-ref name="defaultStack"></interceptor-ref>
<!-- 引用自定义的拦截器,在上面要定义 -->
<interceptor-ref name="abstractInterceptorTest"></interceptor-ref>
<interceptor-ref name="testInterceptor"></interceptor-ref>
</action>
</package>
default-stack
还是要放在最上面<package name="test" extends="struts-default" namespace="/">
<!-- 定义拦截器 -->
<interceptors>
<!-- 实现接口的拦截器 -->
<interceptor name="testInterceptor" class="com.jsnu.struts2.Interceptor.TestInterceptor"></interceptor>
<!-- 继承类的 -->
<interceptor name="abstractInterceptorTest" class="com.jsnu.struts2.Interceptor.AbstractInterceptorTest"></interceptor>
<!-- 自定拦截器栈,其中引用了上面的两个拦截器 -->
<interceptor-stack name="myStack">
<!-- 引用自定义的拦截器,在上面要定义 -->
<interceptor-ref name="abstractInterceptorTest"></interceptor-ref>
<interceptor-ref name="testInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="simple" class="com.jsnu.struts2.controller.SimpleAction">
<result name="success">/jsp/success.jsp</result>
<result name="input">/jsp/input.jsp</result>
<!-- 在使用了自定的拦截器之后,那么系统默认的拦截器栈将会失去作用,因此这里需要重新指定拦截器栈-->
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="myStack"></interceptor-ref>
</action>
</package>
<package name="test" extends="struts-default" namespace="/">
<!-- 定义拦截器 -->
<interceptors>
<!-- 实现接口的拦截器 -->
<interceptor name="testInterceptor" class="com.jsnu.struts2.Interceptor.TestInterceptor"></interceptor>
<!-- 继承类的 -->
<interceptor name="abstractInterceptorTest" class="com.jsnu.struts2.Interceptor.AbstractInterceptorTest"></interceptor>
<!-- 自定拦截器栈,其中引用了上面的两个拦截器 -->
<interceptor-stack name="myStack">
<!-- 在使用了自定的拦截器之后,那么系统默认的拦截器栈将会失去作用,因此这里需要重新指定拦截器栈-->
<interceptor-ref name="defaultStack"></interceptor-ref>
<!-- 引用自定义的拦截器,在上面要定义 -->
<interceptor-ref name="abstractInterceptorTest"></interceptor-ref>
<interceptor-ref name="testInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="simple" class="com.jsnu.struts2.controller.SimpleAction">
<result name="success">/jsp/success.jsp</result>
<result name="input">/jsp/input.jsp</result>
<interceptor-ref name="myStack"></interceptor-ref>
</action>
</package>
<struts>
<package name="test" extends="struts-default" namespace="/">
<!-- 定义拦截器 -->
<interceptors>
<!-- 实现接口的拦截器 -->
<interceptor name="testInterceptor" class="com.jsnu.struts2.Interceptor.TestInterceptor"></interceptor>
<!-- 继承类的 -->
<interceptor name="abstractInterceptorTest" class="com.jsnu.struts2.Interceptor.AbstractInterceptorTest"></interceptor>
<!-- 自定拦截器栈,其中引用了上面的两个拦截器 -->
<interceptor-stack name="myStack">
<!-- 在使用了自定的拦截器之后,那么系统默认的拦截器栈将会失去作用,因此这里需要重新指定拦截器栈-->
<interceptor-ref name="defaultStack"></interceptor-ref>
<!-- 引用自定义的拦截器,在上面要定义 -->
<interceptor-ref name="abstractInterceptorTest"></interceptor-ref>
<interceptor-ref name="testInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 定义默认的拦截器栈,其中引用了上面定义的拦截器栈 -->
<default-interceptor-ref name="myStack"></default-interceptor-ref>
<action name="simple" class="com.jsnu.struts2.controller.SimpleAction">
<result name="success">/jsp/success.jsp</result>
<result name="input">/jsp/input.jsp</result>
</action>
</package>
</struts>
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class SimpleActionInteceptor extends MethodFilterInterceptor{
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
System.out.println("拦截方法的拦截器起了作用");
String result=invocation.invoke();
System.out.println("执行之后");
return result;
}
}
<param name="excludeMethods">login</param>
用来定义不拦截的方法<param name="includeMethods">regist</param>
用来定义拦截的方法,如果有多个使用逗号隔开method
的,但是我们也可以使用 action!方法名
action!方法名
进行调用,具体方法前面有介绍,开启之后,我们在地址栏输入 http://localhost:8080/web1/simple.regist
,将会成功被拦截器拦截器,但是我们输入http://localhost:8080/web1/simple.login
,拦截器不起作用<package name="test" extends="struts-default" namespace="/">
<!-- 定义拦截器 -->
<interceptors>
<!-- 配置拦截方法的拦截器 -->
<interceptor name="simpleMethod" class="com.jsnu.struts2.Interceptor.SimpleActionInteceptor"></interceptor>
</interceptors>
<action name="simple" class="com.jsnu.struts2.controller.SimpleAction" method="regist">
<result name="success">/jsp/success.jsp</result>
<result name="input">/jsp/input.jsp</result>
<!-- 在使用了自定的拦截器之后,那么系统默认的拦截器栈将会失去作用,因此这里需要重新指定拦截器栈-->
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="simpleMethod">
<!-- 定义不拦截login方法 -->
<param name="excludeMethods">login</param>
<!-- 定义需要拦截器的方法 -->
<param name="includeMethods">regist</param>
</interceptor-ref>
</action>
</package>
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。