struts2學習筆記之十三:自訂過濾器

來源:互聯網
上載者:User

標籤:return   public   開始   string   print   ring   筆記   ppi   適配器   

Struts2的攔截器1、Struts2的攔截器只能攔截Action,攔截器是AOP的一種思路,可以使我們的系統架構更鬆散(耦合度低),可以插拔,容易互換,代碼不改變的情況下很容易滿足客戶需求其實體現了OCP 2、如何?攔截器?(整個攔截器體現了責任鏈模式,Filter也體現了責任鏈模式)* 繼承AbstractInterceptor(體現了預設適配器模式,建議使用該模式)* 實現Interceptor 3、如果自定了攔截器,預設攔截器會失效,必須顯示引用Struts2預設的攔截器 4、攔截器棧,多個攔截器的和 5、定義預設攔截器<default-interceptor-ref>,所有的Action都會使用 6、攔截器的執行原理,在ActionInvocation中有一個成員變數Iterator,這個Iterator中儲存了所有的攔截器,每次都會取得Iterator進行next,如果找到了攔截器就會執行,否則就執行Action,都執行完了攔截器出棧(其實出棧就是攔截器的intercept方法彈出棧) 自訂攔截器myLogInterceptor.java
package com.djoker.struts2;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;public class myLogInterceptor extends AbstractInterceptor {    @Override    public String intercept(ActionInvocation invocation) throws Exception {        System.out.println("記錄日誌開始");                String recode = invocation.invoke();                System.out.println("記錄日誌結束");                        return recode;    }}

 

 

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="user" extends="struts-default" namespace="/user">        <interceptors>            <!-- 聲明過濾器 -->            <interceptor name="mylog" class="com.djoker.struts2.myLogInterceptor"></interceptor>            <!-- 使用站方式聲明過濾器 -->            <interceptor-stack name="myStack">                <interceptor-ref name="defaultStack"></interceptor-ref>                <interceptor-ref name="mylog"></interceptor-ref>            </interceptor-stack>        </interceptors>        <!-- 預設攔截器,如果沒有指定則使用該攔截器 -->        <default-interceptor-ref name="myStack"></default-interceptor-ref>        <global-exception-mappings>            <exception-mapping result="error" exception="com.djoker.struts2.UserNotFoundException"></exception-mapping>        </global-exception-mappings>        <action name="*User" class="com.djoker.struts2.UserAction" method="{1}User">            <!-- 普通配置,但是每個Action都需要配置            <interceptor-ref name="defaultStack"></interceptor-ref>            <interceptor-ref name="mylog"></interceptor-ref>             -->             <!--               <interceptor-ref name="mylog"></interceptor-ref>             -->            <exception-mapping result="error" exception="com.djoker.struts2.UserNotFoundException"></exception-mapping>            <result>/success.jsp</result>            <result name="error">/error.jsp</result>        </action>    </package></struts>

 

struts2學習筆記之十三:自訂過濾器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.