成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

淺談Struts2攔截器的原理與實(shí)現(xiàn)

開發(fā) 后端
Struts2攔截器是Struts2中的一個很重要的功能。本文將從概念開始,為大家講解Struts2攔截器的實(shí)現(xiàn)原理以及如何定義等等內(nèi)容

一、理解Struts2攔截器

1. Struts2攔截器是在訪問某個Action或Action的某個方法,字段之前或之后實(shí)施攔截,并且Struts2攔截器是可插拔的,攔截器是AOP的一種實(shí)現(xiàn).

2. 攔截器棧(Interceptor Stack)。Struts2攔截器棧就是將攔截器按一定的順序聯(lián)結(jié)成一條鏈。在訪問被攔截的方法或字段時,Struts2攔截器鏈中的攔截器就會按其之前定義的順序被調(diào)用。

二、實(shí)現(xiàn)Struts2攔截器原理

Struts2攔截器的實(shí)現(xiàn)原理相對簡單,當(dāng)請求struts2的action時,Struts 2會查找配置文件,并根據(jù)其配置實(shí)例化相對的    攔截器對象,然后串成一個列表,最后一個一個地調(diào)用列表中的攔截器

三、定義Struts2攔截器。

Struts2規(guī)定用戶自定義攔截器必須實(shí)現(xiàn)com.opensymphony.xwork2.interceptor.Interceptor接口。該接口聲明了3個方法,

void init();
void destroy();
String intercept(ActionInvocation invocation) throws Exception;

其中,init和destroy方法會在程序開始和結(jié)束時各執(zhí)行一遍,不管使用了該攔截器與否,只要在struts.xml中聲明了該Struts2攔截器就會被執(zhí)行。
intercept方法就是攔截的主體了,每次攔截器生效時都會執(zhí)行其中的邏輯。

不過,struts中又提供了幾個抽象類來簡化這一步驟。

public abstract class AbstractInterceptor implements Interceptor;
public abstract class MethodFilterInterceptor extends AbstractInterceptor;

都是模板方法實(shí)現(xiàn)的。

其中AbstractInterceptor提供了init()和destroy()的空實(shí)現(xiàn),使用時只需要覆蓋intercept()方法;

而MethodFilterInterceptor則提供了includeMethods和excludeMethods兩個屬性,用來過濾執(zhí)行該過濾器的action的方法??梢酝ㄟ^param來加入或者排除需要過濾的方法。

一般來說,攔截器的寫法都差不多??聪旅娴氖纠?/P>

package interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class MyInterceptor implements Interceptor {
public void destroy() {
  // TODO Auto-generated method stub
}
public void init() {
// TODO Auto-generated method stub
}
public String intercept(ActionInvocation invocation) throws Exception {
   System.out.println("Action執(zhí)行前插入 代碼");     
        //執(zhí)行目標(biāo)方法 (調(diào)用下一個攔截器, 或執(zhí)行Action)   
        final String res = invocation.invoke();   
        System.out.println("Action執(zhí)行后插入 代碼");   
        return res;   
}
}

四、配置Struts2攔截器

Struts2攔截器需要在struts.xml中聲明,如下struts.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "<struts>
<constant name="struts.objectFactory" value="spring" />

<package name="default" extends="struts-default">
<interceptors>
<interceptor name="MyInterceptor" class="interceptor.MyInterceptor"></interceptor>
<interceptor-stack name="myInterceptorStack">
<interceptor-ref name="MyInterceptor"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
   <action name="loginAction" class="loginAction">
    <result name="fail">/index.jsp </result>
    <result name="success">/success.jsp</result>
   <interceptor-ref name="myInterceptorStack"></interceptor-ref>
   </action>
</package>
</struts>

 

攔截器

名字

說明

Alias Interceptor

alias

在不同請求之間將請求參數(shù)在不同名字件轉(zhuǎn)換,請求內(nèi)容不變

Chaining Interceptor

chain

讓前一個Action的屬性可以被后一個Action訪問,現(xiàn)在和chain類型的result)結(jié)合使用。

Checkbox Interceptor

checkbox

添加了checkbox自動處理代碼,將沒有選中的checkbox的內(nèi)容設(shè)定為false,而html默認(rèn)情況下不提交沒有選中的checkbox。

Cookies Interceptor

cookies

使用配置的name,value來是指cookies

Conversion Error Interceptor

conversionError

將錯誤從ActionContext中添加到Action的屬性字段中。

Create Session Interceptor

createSession

自動的創(chuàng)建HttpSession,用來為需要使用到HttpSession的攔截器服務(wù)。

Debugging Interceptor

debugging

提供不同的調(diào)試用的頁面來展現(xiàn)內(nèi)部的數(shù)據(jù)狀況。

Execute and Wait Interceptor

execAndWait

在后臺執(zhí)行Action,同時將用戶帶到一個中間的等待頁面。

Exception Interceptor

exception

將異常定位到一個畫面

File Upload Interceptor

fileUpload

提供文件上傳功能

I18n Interceptor

i18n

記錄用戶選擇的locale

Logger Interceptor

logger

輸出Action的名字

Message Store Interceptor

store

存儲或者訪問實(shí)現(xiàn)ValidationAware接口的Action類出現(xiàn)的消息,錯誤,字段錯誤等。

Model Driven Interceptor

model-driven

如果一個類實(shí)現(xiàn)了ModelDriven,將getModel得到的結(jié)果放在Value Stack中。

Scoped Model Driven

scoped-model-driven

如果一個Action實(shí)現(xiàn)了ScopedModelDriven,則這個攔截器會從相應(yīng)的Scope中取出model調(diào)用ActionsetModel方法將其放入Action內(nèi)部。

Parameters Interceptor

params

將請求中的參數(shù)設(shè)置到Action中去。

Prepare Interceptor

prepare

如果Acton實(shí)現(xiàn)了Preparable,則該攔截器調(diào)用Action類的prepare方法。

Scope Interceptor

scope

Action狀態(tài)存入sessionapplication的簡單方法。

Servlet Config Interceptor

servletConfig

提供訪問HttpServletRequestHttpServletResponse的方法,以Map的方式訪問。

Static Parameters Interceptor

staticParams

struts.xml文件中將中的中的內(nèi)容設(shè)置到對應(yīng)的Action中。

Roles Interceptor

roles

確定用戶是否具有JAAS指定的Role,否則不予執(zhí)行。

Timer Interceptor

timer

輸出Action執(zhí)行的時間

Token Interceptor

token

通過Token來避免雙擊

Token Session Interceptor

tokenSession

Token Interceptor一樣,不過雙擊的時候把請求的數(shù)據(jù)存儲在Session

Validation Interceptor

validation

使用action-validation.xml文件中定義的內(nèi)容校驗(yàn)提交的數(shù)據(jù)。

Workflow Interceptor

workflow

調(diào)用Actionvalidate方法,一旦有錯誤返回,重新定位到INPUT畫面

Parameter Filter Interceptor

N/A

從參數(shù)列表中刪除不必要的參數(shù)

Profiling Interceptor

profiling

通過參數(shù)激活profile


【編輯推薦】

  1. 在Eclipse中開發(fā)struts應(yīng)用程序
  2. 手把手教你在Eclipse中配置開發(fā)Struts
  3. Eclipse下開發(fā)struts完整解決亂碼問題
  4. Struts相關(guān)背景介紹
  5. 使用Easy Struts for Eclipse開發(fā)Struts
責(zé)任編輯:張燕妮 來源: 網(wǎng)易博客
相關(guān)推薦

2009-06-25 15:59:21

Struts2教程攔截器

2009-06-25 15:54:42

Struts2教程攔截器

2009-02-04 14:45:06

2009-02-04 14:19:38

2009-06-08 16:44:00

2009-06-04 08:45:01

Struts2下載

2009-06-08 16:44:00

Struts2文件上傳

2009-06-04 09:41:50

struts2上傳文件

2011-06-28 09:14:23

Struts 2WebWork

2009-06-05 09:58:20

struts2驗(yàn)證用戶注冊

2011-05-13 09:53:02

strutsAjax

2011-04-28 09:52:04

Struts2

2009-07-29 09:54:34

struts2和str

2010-01-06 14:36:04

JSON插件

2009-06-08 16:44:00

struts2 ogn

2024-05-06 00:00:00

C#工具代碼

2012-04-25 10:14:40

JavaStruts

2023-09-05 08:58:07

2009-06-25 15:11:28

Struts2教程Struts2程序

2011-05-16 10:14:11

Hibernate
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 久久久久91| 毛片日韩 | 精品福利在线视频 | 午夜精品久久久久久久99黑人 | 国产成人精品久久二区二区91 | aaaa网站 | 国产精品久久久久久久一区探花 | 日韩黄色免费 | 黄网免费 | 国产欧美一区二区精品久导航 | 免费欧美 | 日韩精品久久久久久 | 91中文字幕| 国产精品久久久久久吹潮 | 青青草一区 | 懂色av一区二区三区在线播放 | 黄色永久免费 | 在线免费观看一区二区 | 日韩乱码一二三 | 亚洲精品一区二区三区蜜桃久 | 天天操天天天干 | 日韩欧美亚洲 | 欧美日本韩国一区二区三区 | 精品久久久久久亚洲精品 | 亚洲精品一区二区三区四区高清 | 成人3d动漫一区二区三区91 | 久久小视频 | 国产精品久久久久久久一区探花 | 五月婷亚洲 | 国产精品成人一区二区三区夜夜夜 | 成人三级视频 | 成人深夜福利 | 欧美亚洲视频在线观看 | 亚洲性视频| 欧美精品国产一区二区 | 久久一二 | 九九亚洲 | 一区二区视频 | 亚洲成人国产 | 久久久久久久久久久高潮一区二区 | 国产九一精品 |