struts2学习札记十一(第11讲.Struts2的核心 拦截器续)
struts2学习笔记十一(第11讲.Struts2的核心 拦截器续)
Struts2的核心 拦截器续
说明:拦截器的主要作用就是拦截Action,执行顺序就是:拦截器->Action->拦截器->……。
功能:拦截器的基本的配置。
一、在之前的struts2项目src下创建一个包com.test.interceptor,用来存放拦截器,创建一个名称为MyInterceptor.java类,实现Interceptor接口:
二、在struts.xml配置文件中配置拦截器,在package节点先添加interceptors节点,并且要在对应的Action中进行引用:
三、再在struts.xml中的Intercepoters节点中添加一个param参数:
四、然后在拦截器MyInterceptor.java中添加一个对应的对象hello:
结果是world能在控制台正常的输出:init()
world
五、然后在Action中引用的时候,在在引用的属性中再增加一个param属性,并且把welcome赋给hello:
在控制台输出的是:init()
welcome,说明后来所赋得值welcome把前面的给替换掉了,所以说使用的时候给变量赋的值,会替换掉之前定义的时候所赋得值。
功能:定义拦截器栈。
六、还是在Intercepoters借点节点中添加一个拦截器栈,然后在对应的Action中引用这个拦截器栈,效果是一样的:
功能:改变默认继承的拦截器:
七、添加一个默认的拦截器栈,替换原有默认的拦截器,可以应用到所有的Action当中:
功能:在同一目录下再定义一个拦截器MyInterceptor2.java继承自AbstractInterceptor类:
八、在struts.xml中配置一下:
说明:拦截器执行的先后顺序与拦截器配置的先后顺序有关系,但是在结束的时候后配置的拦截器先结束。
功能:方法拦截器的实现。
九、在com.test.interceptor创建方法拦截器的类MyInterceptor3.java继承自MethodFilterInterceptor父类:
十、在struts.xml中进行方法拦截器的配置:
说明:includeMethods的属性具有优先级,但对某方法既拦截又不拦截的时候,这个方法是一定会执行的。
Struts2的核心 拦截器续
说明:拦截器的主要作用就是拦截Action,执行顺序就是:拦截器->Action->拦截器->……。
功能:拦截器的基本的配置。
一、在之前的struts2项目src下创建一个包com.test.interceptor,用来存放拦截器,创建一个名称为MyInterceptor.java类,实现Interceptor接口:
package com.test.interceptor; import java.awt.event.InvocationEvent; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class MyInterceptor implements Interceptor { public void destroy() { System.out.println("destroy()"); } public void init() { System.out.println("init()"); } public String intercept(ActionInvocation invocation) throws Exception { System.out.println("intercept"); String result = invocation.invoke(); return result; } }
二、在struts.xml配置文件中配置拦截器,在package节点先添加interceptors节点,并且要在对应的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> <constant name="struts.custom.i18n.resources" value="message"> </constant> <package name="struts2" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"> </interceptor> </interceptors> <action name="login" class="com.test.action.LoginAction"> <result name="input">/login2.jsp</result> <result name="success">/result.jsp</result> <result name="failer">login2.jsp</result> </action> <action name="pointConverter" class="com.test.action.PointAction"> <result name="success">/output.jsp</result> </action> <action name="register" class="com.test.action.RegisterAction"> <result name="success">/success.jsp</result> <result name="input">/register2.jsp</result> <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action> <action name="zly" class="com.test.action.RegisterAction" method="test"> <result name="success">/success.jsp</result> <result name="input">/register2.jsp</result> </action> </package> </struts>
三、再在struts.xml中的Intercepoters节点中添加一个param参数:
<interceptors> <interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"> <param name="hello">world</param> </interceptor> </interceptors>
四、然后在拦截器MyInterceptor.java中添加一个对应的对象hello:
public void init() { System.out.println("init()"); System.out.println(hello); }
结果是world能在控制台正常的输出:init()
world
五、然后在Action中引用的时候,在在引用的属性中再增加一个param属性,并且把welcome赋给hello:
<action name="register" class="com.test.action.RegisterAction"> <result name="success">/success.jsp</result> <result name="input">/register2.jsp</result> <interceptor-ref name="myInterceptor"> <param name="hello">welcome</param> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action>
在控制台输出的是:init()
welcome,说明后来所赋得值welcome把前面的给替换掉了,所以说使用的时候给变量赋的值,会替换掉之前定义的时候所赋得值。
功能:定义拦截器栈。
六、还是在Intercepoters借点节点中添加一个拦截器栈,然后在对应的Action中引用这个拦截器栈,效果是一样的:
<package name="struts2" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"> <param name="hello">world</param> </interceptor> <interceptor-stack name="myStack"> <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <action name="login" class="com.test.action.LoginAction"> <result name="input">/login2.jsp</result> <result name="success">/result.jsp</result> <result name="failer">login2.jsp</result> </action> <action name="pointConverter" class="com.test.action.PointAction"> <result name="success">/output.jsp</result> </action> <action name="register" class="com.test.action.RegisterAction"> <result name="success">/success.jsp</result> <result name="input">/register2.jsp</result> <interceptor-ref name="myStack"></interceptor-ref> </action> </package>
功能:改变默认继承的拦截器:
七、添加一个默认的拦截器栈,替换原有默认的拦截器,可以应用到所有的Action当中:
<package name="struts2" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"> <param name="hello">world</param> </interceptor> <interceptor-stack name="myStack"> <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="myStack"></default-interceptor-ref> <action name="login" class="com.test.action.LoginAction"> <result name="input">/login2.jsp</result> <result name="success">/result.jsp</result> <result name="failer">login2.jsp</result> </action> <action name="pointConverter" class="com.test.action.PointAction"> <result name="success">/output.jsp</result> </action> <action name="register" class="com.test.action.RegisterAction"> <result name="success">/success.jsp</result> <result name="input">/register2.jsp</result> </action> </package>
功能:在同一目录下再定义一个拦截器MyInterceptor2.java继承自AbstractInterceptor类:
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.out.println("Intercept2"); String result = invocation.invoke(); return result; } }
八、在struts.xml中配置一下:
<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="login" class="com.test.action.LoginAction"> <result name="input">/login2.jsp</result> <result name="success">/result.jsp</result> <result name="failer">login2.jsp</result> </action> <action name="pointConverter" class="com.test.action.PointAction"> <result name="success">/output.jsp</result> </action> <action name="register" class="com.test.action.RegisterAction" method="test"> <result name="success">/success.jsp</result> <result name="input">/register2.jsp</result> <interceptor-ref name="myInterceptor3"> <!-- <param name="includeMethods">test,execute</param> --> <param name="excludeMethods">test,execute</param> <param name="includeMethods">test</param> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action> </package>
说明:拦截器执行的先后顺序与拦截器配置的先后顺序有关系,但是在结束的时候后配置的拦截器先结束。
功能:方法拦截器的实现。
九、在com.test.interceptor创建方法拦截器的类MyInterceptor3.java继承自MethodFilterInterceptor父类:
package com.test.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; public class MyInterceptor3 extends MethodFilterInterceptor { public void init(){ System.out.println("init3"); } @Override protected String doIntercept(ActionInvocation invocation) throws Exception { System.out.println("my interceptor3"); String result = invocation.invoke(); return result; } }
十、在struts.xml中进行方法拦截器的配置:
<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="login" class="com.test.action.LoginAction"> <result name="input">/login2.jsp</result> <result name="success">/result.jsp</result> <result name="failer">login2.jsp</result> </action> <action name="pointConverter" class="com.test.action.PointAction"> <result name="success">/output.jsp</result> </action> <action name="register" class="com.test.action.RegisterAction" method="test"> <result name="success">/success.jsp</result> <result name="input">/register2.jsp</result> <interceptor-ref name="myInterceptor3"> <!-- <param name="includeMethods">test,execute</param> --> <param name="excludeMethods">test,execute</param> <param name="includeMethods">test</param> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action> </package>
说明:includeMethods的属性具有优先级,但对某方法既拦截又不拦截的时候,这个方法是一定会执行的。