Struts2之攔截器的簡單應用

在上一篇文章中說了攔截器的原理,但是沒有實踐是永遠不行的,所以下面就來看看攔截器是如何工作的:
無非也就是那麼幾個步驟:
        1、編寫頁面jsp
        2、配置Action
        3、編寫攔截器Interceptor
        4、在配置文件中配置攔截器及要攔截的對象。
一、攔截器的簡單應用
1、Login.jsp
     <form action="LoginAction.action">

            username:<input type="text" name="username"><br>

            password:<input type="text" name="password"><br>

            age:<input type="text" name="age"><br>

            date:<input type="text" name="date"><br>

            <input type="submit" value="submit">

      </form>
2、LoginAction
        public class LoginAction extends ActionSupport implements ModelDriven<Person> {

            private Person person=new Person();

            @Override
            public Person getModel() {

                return person;

            }

            @Override
            public String execute() throws Exception {

                return SUCCESS;

            } 

        }
3、Interceptor
        public class TheInterceptor implements Interceptor {

            private String testString;
            //get、set方法省略...
            
            @Override
            public void destroy() {
                // TODO Auto-generated method stub
            }

            @Override
            public void init() {
                System.out.println("init invoked!");
                System.out.println(testString);  
            }

            @Override
            public String intercept(ActionInvocation invocation) throws Exception {

                System.out.println("before!");

                String result= invocation.invoke();

                System.out.println("after!");

                return null;

            }

        }
4、配置struts.xml
    <struts>
        <package name="struts2" extends="struts-default">

            <!-- 配置interceptor -->
            <interceptors>
                <interceptor name="theInterceptor" class="com.tgb.interceptor.TheInterceptor">
                    <param name="testString">tgb</param>
                </interceptor>
            </interceptors>
            
            <action name="LoginAction" class="com.tgb.struts2.LoginAction">
                <result name="success">/success.jsp</result>
                <result name="input">/index.jsp</result>

                  <!-- 引用攔截器 -->
                <interceptor-ref name="theInterceptor"></interceptor-ref>
                <interceptor-ref name="defaultStack"></interceptor-ref>
            </action>
        </package>
    </struts> 

二、其餘的攔截器
        在上面的應用中我們直接實現的的Interceptor接口,在這個接口中我們要實現三個方法:destroy,init,intercept。除此之外還有另外兩個攔截器,如下:
    1)AbstractInterceptor,其實現接口Interceptor。如果沒有特別需求的話,我們只需要繼承AbstractInterceptor類,然後在其中編寫intercept方法即可。
    2) MethodFilterInterceptor,期繼承抽象類AbstractInterceptor。其主要是針對方法的,其中封裝了兩個參數excludeMethods和includeMethods。其含義:不包含方法和包含方法。意思是如果配置了這兩個參數,當攔截時根據配置的不同而對方法處理時採取不攔截和攔截策略。
    3)上面兩種在struts中的配置如下:
 
<package name="struts2" extends="struts-default">
        <interceptors>
             <!-- 繼承抽象類AbstractInterceptor -->
            <interceptor name="theInterceptor2" class="com.tgb.interceptor.TheInterceptor2">
            </interceptor>
              <!-- 繼承抽象類 MethodFilterInterceptor  -->
            <interceptor name="theInterceptor3" class="com.tgb.interceptor.TheInterceptor3">
            </interceptor>
        </interceptors>
        
        <action name="LoginAction" class="com.tgb.struts2.LoginAction">
            <result name="success">/success.jsp</result>
            <result name="input">/index.jsp</result>
            <interceptor-ref name="theInterceptor2"></interceptor-ref>
            <interceptor-ref name="theInterceptor3">
                  <!-- 配置參數是否攔截該方法 -->
                <param name="includeMethods">execute</param>
            </interceptor-ref>
            <interceptor-ref name="defaultStack"></interceptor-ref>
        </action>
    </package>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章