中進行定義。下面是Struts2中的內(nèi)置攔截器介紹InterceptorNameDescriptionAliasInterceptoralias在不同的請求之間將參數(shù)在不同的名字間轉(zhuǎn)換,請求內(nèi)容不變.Chaining" />

欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Struts2 攔截器總結(jié)(內(nèi)置和新建)

系統(tǒng) 1742 0

??? 攔截器的類已經(jīng)定義在特殊的配置文件中,這個配置文件的名字就叫做struts-default.xml,如果你繼承了struts-default默認(rèn)的包名,那你就可以使用這些攔截器了,否則你必須在你自己的包中定義攔截器在<interceptors>中進行定義。


下面是Struts2中的內(nèi)置攔截器介紹

Interceptor Name Description
Alias Interceptor alias 在不同的請求之間將參數(shù)在不同的名字間轉(zhuǎn)換,請求內(nèi)容不變.
Chaining Interceptor chain 讓前面一個Action的屬性可以被后一個Action訪問
Checkbox Interceptor checkbox Adds automatic checkbox handling code that detect an unchecked checkbox and add it as a parameter with a default (usually 'false') value. Uses a specially named hidden field to detect unsubmitted checkboxes. The default unchecked value is overridable for non-boolean value'd checkboxes.
Cookie Interceptor cookie 使用配置的name、value來設(shè)置cookies
Conversion Error Interceptor conversionError 將錯誤從ActionContext中添加到Action的屬性字段中
Create Session Interceptor createSession 自動地創(chuàng)建HttpSession,用來為需要使用到HttpSession的攔截器服務(wù)
DebuggingInterceptor debugging 提供不同的調(diào)試用的頁面來展示內(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 存儲或者訪問實現(xiàn)ValidationAware接口的Action類出現(xiàn)的消息、錯誤、字段錯誤等。
Model Driven Interceptor model-driven 如果一個類實現(xiàn)了ModelDriven,將getModel得到的結(jié)果放在ValueStack中
Scoped Model Driven Interceptor scoped-model-driven 如果一個Action實現(xiàn)了ScopedModelDriven,則這個攔截器會從相應(yīng)的Scope中取出model,調(diào)用Action的setModel方法將其放入Action內(nèi)部
Parameters Interceptor params 將請求中的參數(shù)設(shè)置到Action中去
Prepare Interceptor prepare 如果Action實現(xiàn)了Preparable,則該攔截器調(diào)用Action類的prepare方法
Scope Interceptor scope 將Action狀態(tài)存入Session和Application的簡單方法
Servlet Config Interceptor servletConfig 提供訪問HttpServletRequest和HttpServletResponse的方法,以Map的方式訪問
Static Parameters Interceptor staticParams 從struts.xml文件中將<action>中的<param>下的內(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 Interceptory一樣,不過雙擊的時候把請求的數(shù)據(jù)存儲在Session中
Validation Interceptor validation 使用action-validation.xml文件中定義的內(nèi)容校驗提交的數(shù)據(jù)
Workflow Interceptor workflow 調(diào)用Action的validate的方法,一旦有錯返回,重新定位到INPUT頁面
Parameter Filter Interceptor N/A 從參數(shù)列表中刪除不必要的參數(shù)
Profiling Interceptor profiling 通過參數(shù)激活profile

?

?

?

? 每一個攔截器都可以配置參數(shù),有兩種方式配置參數(shù),一是針對每一個攔截器定義參數(shù),二是針對一個攔截器堆棧統(tǒng)一定義所有的參數(shù),例如:

<interceptor-ref name="validation"> <param name="excludeMethods">myValidationExcudeMethod</param> </interceptor-ref> <interceptor-ref name="workflow"> <param name="excludeMethods">myWorkflowExcludeMethod</param> </interceptor-ref>

  

?

?

?

或者

<interceptor-ref name="defaultStack"> <param name="validation.excludeMethods">myValidationExcludeMethod</param> <param name="workflow.excludeMethods">myWorkflowExcludeMethod</param> </interceptor-ref>

  
    
      

?

?

每一個攔截器都有兩個默認(rèn)的參數(shù):

excludeMethods - 過濾掉不使用攔截器的方法和

includeMethods – 使用攔截器的方法。

?

需要說明的幾點:

1 攔截器執(zhí)行的順序按照定義的順序執(zhí)行,例如:

?

<interceptor-stack name="xaStack"> <interceptor-ref name="thisWillRunFirstInterceptor"/> <interceptor-ref name="thisWillRunNextInterceptor"/> <interceptor-ref name="followedByThisInterceptor"/> <interceptor-ref name="thisWillRunLastInterceptor"/> </interceptor-stack>

  
    
      

?

?

的執(zhí)行順序為:

?

thisWillRunFirstInterceptor thisWillRunNextInterceptor followedByThisInterceptor thisWillRunLastInterceptor MyAction1 MyAction2 (chain) MyPreResultListener MyResult (result) thisWillRunLastInterceptor followedByThisInterceptor thisWillRunNextInterceptor thisWillRunFirstInterceptor

  

?

?

?

2 使用默認(rèn)攔截器配置每個 Action 都需要的攔截器堆棧,例如:

?

?

<action name="login" class="tutorial.Login"> <interceptor-ref name="timer"/> <interceptor-ref name="logger"/> <interceptor-ref name="default-stack"/> <result name="input">login.jsp</result> <result type="redirect-action">/secure/home</result> </action>

  
  

?

可以按照如下的方式定義:

?

?

<interceptors> <interceptor-stack name="myStack"> <interceptor-ref name="timer"/> <interceptor-ref name="logger"/> <interceptor-ref name="default-stack"/> </interceptor-stack> </interceptors> <default-interceptor-ref name="myStack"/> <action name="login" class="tutorial.Login"> <result name="input">login.jsp</result> <result type="redirect-action">/secure/home</result> </action>

  

?

?

自定義攔截器

??? 作為“框架(framework)”,可擴展性是不可或缺的,因為世上沒有放之四海而皆準(zhǔn)的東西。雖然,Struts 2為我們提供如此豐富的攔截器實現(xiàn),但是這并不意味我們失去創(chuàng)建自定義攔截器的能力,恰恰相反,在Struts 2自定義攔截器是相當(dāng)容易的一件事。

?

?

大家在開始著手創(chuàng)建自定義攔截器前,切記以下原則:
攔截器必須是無狀態(tài)的,不要使用在API提供的ActionInvocation之外的任何東西。

??? 要求攔截器是無狀態(tài)的原因是Struts 2不能保證為每一個請求或者action創(chuàng)建一個實例,所以如果攔截器帶有狀態(tài),會引發(fā)并發(fā)問題。

??? 所有的Struts 2的攔截器都直接或間接實現(xiàn)接口com.opensymphony.xwork2.interceptor.Interceptor。除此之外,大家可能更喜歡繼承類com.opensymphony.xwork2.interceptor.AbstractInterceptor。

以下例子演示通過繼承AbstractInterceptor,實現(xiàn)授權(quán)攔截器。

首先,創(chuàng)建授權(quán)攔截器類tutorial.AuthorizationInterceptor,代碼如下:

package tutorial;

import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class AuthorizationInterceptor extends AbstractInterceptor {

? ?@Override
? ?
public String intercept(ActionInvocation ai) throws Exception {
? ? ? ?Map session
= ai.getInvocationContext().getSession();
? ? ? ?String role
= (String) session.get( " ROLE " );
? ? ? ?
if ( null != role) {
? ? ? ? ? ?Object o
= ai.getAction();
? ? ? ? ? ?
if (o instanceof RoleAware) {
? ? ? ? ? ? ? ?RoleAware action
= (RoleAware) o;
? ? ? ? ? ? ? ?action.setRole(role);
? ? ? ? ? ?}

? ? ? ? ? ?
return ai.invoke();
? ? ? ?}
else {
? ? ? ? ? ?
return Action.LOGIN;
? ? ? ?}
? ? ? ?
? ?}


}

??

?? 以上代碼相當(dāng)簡單,我們通過檢查session是否存在鍵為“ROLE”的字符串,判斷用戶是否登陸。如果用戶已經(jīng)登陸,將角色放到Action中,調(diào)用Action;否則,攔截直接返回Action.LOGIN字段。為了方便將角色放入Action,我定義了接口tutorial.RoleAware,代碼如下:

package tutorial;

public interface RoleAware {
? ?
void setRole(String role);
}

?

?

?? 接著,創(chuàng)建Action類tutorial.AuthorizatedAccess模擬訪問受限資源,它作用就是通過實現(xiàn)RoleAware獲取角色,并將其顯示到ShowUser.jsp中,代碼如下:

package tutorial;

import com.opensymphony.xwork2.ActionSupport;

public class AuthorizatedAccess extends ActionSupport implements RoleAware {
? ?
private String role;
? ?
? ?
public void setRole(String role) {
? ? ? ?
this .role = role;
? ?}

? ?
? ?
public String getRole() {
? ? ? ?
return role;
? ?}


? ?@Override
? ?
public String execute() {
? ? ? ?
return SUCCESS;
? ?}

}

?

?

?? 以下是ShowUser.jsp的代碼:

<% @ page ?contentType = " text/html; charset=UTF-8 " %>
<% @taglib prefix = " s " uri = " /struts-tags " %>
< html >
< head >
? ?
< title > Authorizated User </ title >
</ head >
< body >
? ?
< h1 > Your role is: < s:property value ="role" /></ h1 >
</ body >
</ html >

?

??? 然后,創(chuàng)建tutorial.Roles初始化角色列表,代碼如下:

package tutorial;

import java.util.Hashtable;
import java.util.Map;


public class Roles {
? ?
public Map < String, String > getRoles() {
? ? ? ?Map
< String, String > roles = new Hashtable < String, String > ( 2 );
? ? ? ?roles.put(
" EMPLOYEE " , " Employee " );
? ? ? ?roles.put(
" MANAGER " , " Manager " );
? ? ? ?
return roles;
? ?}

}

?

???? 接下來,新建Login.jsp實例化tutorial.Roles,并將其roles屬性賦予<s:radio>標(biāo)志,代碼如下:

<% @ page ?contentType = " text/html; charset=UTF-8 " %>
<% @taglib prefix = " s " uri = " /struts-tags " %>
< html >
< head >
? ?
< title > Login </ title >
</ head >
< body >
? ?
< h1 > Login </ h1 >
? ? Please select a role below:
? ?
< s:bean id ="roles" name ="tutorial.Roles" />
? ?
< s:form action ="Login" >
? ? ? ?
< s:radio list ="#roles.roles" value ="'EMPLOYEE'" name ="role" label ="Role" />
? ? ? ?
< s:submit />
? ?
</ s:form >
</ body >
</ html >

?

??? 創(chuàng)建Action類tutorial.Login將role放到session中,并轉(zhuǎn)到Action類tutorial.AuthorizatedAccess,代碼如下:

package tutorial;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class Login extends ActionSupport implements SessionAware {
? ?
private String role; ? ?
? ?
private Map session;

? ?
public String getRole() {
? ? ? ?
return role;
? ?}


? ?
public void setRole(String role) {
? ? ? ?
this .role = role;
? ?}

? ?
? ?
public void setSession(Map session) {
? ? ? ?
this .session = session;
? ?}


? ?@Override
? ?
public String execute() {
? ? ? ?session.put(
" ROLE " , role);
? ? ? ?
return SUCCESS;
? ?}
? ?
}

???

??? 最后,配置struts.xml文件,內(nèi)容如下:

<! DOCTYPE struts PUBLIC
? ? ? ? "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
? ? ? ? "http://struts.apache.org/dtds/struts-2.0.dtd"
>
< struts >
? ?
< include file ="struts-default.xml" /> ? ?
? ?
< package name ="InterceptorDemo" extends ="struts-default" >
? ? ? ?
< interceptors >
? ? ? ? ? ?
< interceptor name ="auth" class ="tutorial.AuthorizationInterceptor" />
? ? ? ?
</ interceptors >
? ? ? ?
< action name ="Timer" class ="tutorial.TimerInterceptorAction" >
? ? ? ? ? ?
< interceptor-ref name ="timer" />
? ? ? ? ? ?
< result > /Timer.jsp </ result >
? ? ? ?
</ action >
? ? ? ?
< action name ="Login" class ="tutorial.Login" >
? ? ? ? ? ?
< result type ="chain" > AuthorizatedAccess </ result >
? ? ? ?
</ action >
? ? ? ?
< action name ="AuthorizatedAccess" class ="tutorial.AuthorizatedAccess" >
? ? ? ? ? ?
< interceptor-ref name ="auth" />
? ? ? ? ? ?
< result name ="login" > /Login.jsp </ result >
? ? ? ? ? ?
< result name ="success" > /ShowRole.jsp </ result >
? ? ? ?
</ action >
? ?
</ package >
</ struts >

?

?

以上文章是參考網(wǎng)上總結(jié)的,有很多很不錯的文章,在以后可以慢慢研究,先做個記號:

http://longzijian.iteye.com/blog/336840

Struts2 攔截器總結(jié)(內(nèi)置和新建)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产成人精品高清在线观看99 | 五月婷婷综合网 | 亚洲码欧美码一区二区三区 | 日本污污视频在线观看 | 国产三级在线观看 | 亚洲线精品一区二区三区 | 久久精品一区二区三区四区 | 成人免费高清 | 国内一级特黄女人精品片 | xx免费视频 | 国产一区二区精品在线观看 | 一区二区三区免费看 | 美女用震蛋叫爽的视频95视频 | 久久免费看少妇高潮A片JA | 成人欧美一区在线视频在线观看 | 在线成人 | 日韩欧美视频一区二区在线观看 | 超污视频在线看 | 日韩欧美国产中文 | 澳门永久av免费网站 | 成人免费大片黄在线播放 | 欧洲成人一区 | 欧美人成片免费看视频不卡 | 国产高清精品在线 | 午夜家庭影院 | 亚洲精视频 | 免费污的网站 | 天天插天天狠天天透 | 日日a.v拍夜夜添久久免费 | 亚洲国产精品日韩高清秒播 | 久久久国产精品免费观看 | 色欲天天婬色婬香影院 | 桥本有菜免费av一区二区三区 | 日韩一区二区在线观看 | 日本黄色一级片视频 | 国产精品久久久久aaaa九色 | 日本高清视频wwww色 | 免费成人在线网站 | 暖暖日本在线播放 | 欧美日韩中文在线观看 | 一区二区亚洲 |