欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
Struts 2的基石——攔截器(Interceptor)

首先,要跟大家道個(gè)歉,前一陣子為給客戶(hù)個(gè)一個(gè)DEMO,忙得不可開(kāi)交,所以很久沒(méi)有更新Blog。提到這個(gè)DEMO我想順便跟大家分享一下心得——如果大家希望快速開(kāi)發(fā),一個(gè)類(lèi)似Struts 2這樣的簡(jiǎn)單方便的WEB框架必不可少。我們在開(kāi)發(fā)DEMO使用的還是Struts 1.2.8,而且沒(méi)有不使用任何EL(表達式語(yǔ)言),導致頁(yè)面出現無(wú)數類(lèi)似“<%= ((Integer) request.getAttribute("xx")).intValue()%6 %>”的代碼。Struts 1.x的Form Bean的麻煩使得有部分同事直接使用request.getParameter(String arg),繼而引入另一種麻煩。諸如此類(lèi)的問(wèn)題,在DEMO這樣時(shí)間緊迫的項目凸顯了Struts 1.x對快速開(kāi)發(fā)的無(wú)能為力。不過(guò)沒(méi)辦法,由于我們項目中的幾個(gè)資深員工除了Struts 1.x外,對其它的WEB框架似乎不大感興趣。

言歸正傳,Interceptor(以下譯為攔截器)是Struts 2的一個(gè)強有力的工具,有許多功能(feature)都是構建于它之上,如國際化、轉換器,校驗等。

什么是攔截器

攔截器,在A(yíng)OP(Aspect-Oriented Programming)中用于在某個(gè)方法或字段被訪(fǎng)問(wèn)之前,進(jìn)行攔截然后在之前或之后加入某些操作。攔截是AOP的一種實(shí)現策略。

在Webwork的中文文檔的解釋為——攔截器是動(dòng)態(tài)攔截Action調用的對象。它提供了一種機制可以使開(kāi)發(fā)者可以定義在一個(gè)action執行的前后執行的代碼,也可以在一個(gè)action執行前阻止其執行。同時(shí)也是提供了一種可以提取action中可重用的部分的方式。

談到攔截器,還有一個(gè)詞大家應該知道——攔截器鏈(Interceptor Chain,在Struts 2中稱(chēng)為攔截器棧Interceptor Stack)。攔截器鏈就是將攔截器按一定的順序聯(lián)結成一條鏈。在訪(fǎng)問(wèn)被攔截的方法或字段時(shí),攔截器鏈中的攔截器就會(huì )按其之前定義的順序被調用。

實(shí)現原理

Struts 2的攔截器實(shí)現相對簡(jiǎn)單。當請求到達Struts 2的ServletDispatcher時(shí),Struts 2會(huì )查找配置文件,并根據其配置實(shí)例化相對的攔截器對象,然后串成一個(gè)列表(list),最后一個(gè)一個(gè)地調用列表中的攔截器,如圖1所示。


圖1 攔截器調用序列圖

已有的攔截器

Struts 2已經(jīng)為您提供豐富多樣的,功能齊全的攔截器實(shí)現。大家可以到struts2-all-2.0.1.jar或struts2-core-2.0.1.jar包的struts-default.xml查看關(guān)于默認的攔截器與攔截器鏈的配置。

在本文使用是Struts 2的最新發(fā)布版本2.0.1。需要下載的朋友請點(diǎn)擊以下鏈接:
http://apache.justdn.org/struts/binaries/struts-2.0.1-all.zip

以下部分就是從struts-default.xml文件摘取的內容:

< interceptor name ="alias" class ="com.opensymphony.xwork2.interceptor.AliasInterceptor" />
< interceptor name ="autowiring" class ="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor" />
< interceptor name ="chain" class ="com.opensymphony.xwork2.interceptor.ChainingInterceptor" />
< interceptor name ="conversionError" class ="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor" />
< interceptor name ="createSession" class ="org.apache.struts2.interceptor.CreateSessionInterceptor" />
< interceptor name ="debugging" class ="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" />
< interceptor name ="external-ref" class ="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor" />
< interceptor name ="execAndWait" class ="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor" />
< interceptor name ="exception" class ="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor" />
< interceptor name ="fileUpload" class ="org.apache.struts2.interceptor.FileUploadInterceptor" />
< interceptor name ="i18n" class ="com.opensymphony.xwork2.interceptor.I18nInterceptor" />
< interceptor name ="logger" class ="com.opensymphony.xwork2.interceptor.LoggingInterceptor" />
< interceptor name ="model-driven" class ="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor" />
< interceptor name ="scoped-model-driven" class ="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor" />
< interceptor name ="params" class ="com.opensymphony.xwork2.interceptor.ParametersInterceptor" />
< interceptor name ="prepare" class ="com.opensymphony.xwork2.interceptor.PrepareInterceptor" />
< interceptor name ="static-params" class ="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor" />
< interceptor name ="scope" class ="org.apache.struts2.interceptor.ScopeInterceptor" />
< interceptor name ="servlet-config" class ="org.apache.struts2.interceptor.ServletConfigInterceptor" />
< interceptor name ="sessionAutowiring" class ="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor" />
< interceptor name ="timer" class ="com.opensymphony.xwork2.interceptor.TimerInterceptor" />
< interceptor name ="token" class ="org.apache.struts2.interceptor.TokenInterceptor" />
< interceptor name ="token-session" class ="org.apache.struts2.interceptor.TokenSessionStoreInterceptor" />
< interceptor name ="validation" class ="com.opensymphony.xwork2.validator.ValidationInterceptor" />
< interceptor name ="workflow" class ="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor" />
< interceptor name ="store" class ="org.apache.struts2.interceptor.MessageStoreInterceptor" />
< interceptor name ="checkbox" class ="org.apache.struts2.interceptor.CheckboxInterceptor" />
< interceptor name ="profiling" class ="org.apache.struts2.interceptor.ProfilingActivationInterceptor" />

配置和使用攔截器

在struts-default.xml中已經(jīng)配置了以上的攔截器。如果您想要使用上述攔截器,只需要在應用程序struts.xml文件中通過(guò)“<include file="struts-default.xml" />”將struts-default.xml文件包含進(jìn)來(lái),并繼承其中的struts-default包(package),最后在定義Action時(shí),使用“<interceptor-ref name="xx" />”引用攔截器或攔截器棧(interceptor stack)。一旦您繼承了struts-default包(package),所有Action都會(huì )調用攔截器棧 ——defaultStack。當然,在A(yíng)ction配置中加入“<interceptor-ref name="xx" />”可以覆蓋defaultStack。

下面是關(guān)于攔截器timer使用的例子。首先,新建Action類(lèi)tuotrial/TimerInterceptorAction.java,內容如下:

package tutorial;

import com.opensymphony.xwork2.ActionSupport;

public class TimerInterceptorAction extends ActionSupport {
   @Override
   
public String execute() {
       
try {
           
// 模擬耗時(shí)的操作
           Thread.sleep( 500 );
       }
catch (Exception e) {
           e.printStackTrace();
       }

       
return SUCCESS;
   }

}

配置Action,名為T(mén)imer,配置文件如下:

<! 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" >
       
< action name ="Timer" class ="tutorial.TimerInterceptorAction" >
           
< interceptor-ref name ="timer" />
           
< result > /Timer.jsp </ result >
       
</ action >
   
</ package >
</ struts >

至于Timer.jsp可以隨意寫(xiě)些什么到里面。發(fā)布運行應用程序,在瀏覽器的地址欄鍵入http://localhost:8080/Struts2_Interceptor/Timer.action,在出現Timer.jsp頁(yè)面后,查看服務(wù)器的后臺輸出。

2006 - 12 - 6 14 : 27 : 32 com.opensymphony.xwork2.interceptor.TimerInterceptor doLog
信息: Executed action
[ //Timer!execute ] took 2859 ms.

在您的環(huán)境中執行Timer!execute的耗時(shí),可能上述的時(shí)間有些不同,這取決于您PC的性能。但是無(wú)論如何,2859 ms與500 ms還是相差太遠了。這是什么原因呢?其實(shí)原因是第一次加載Timer時(shí),需要進(jìn)行一定的初始工作。當你重新請求Timer.action時(shí),以上輸出會(huì )變?yōu)椋?/p>

2006 - 12 - 6 14 : 29 : 18 com.opensymphony.xwork2.interceptor.TimerInterceptor doLog
信息: Executed action
[ //Timer!execute ] took 500 ms.

OK,這正是我們期待的結果。上述例子演示了攔截器timer的用途——用于顯示執行某個(gè)action方法的耗時(shí),在我們做一個(gè)粗略的性能調試時(shí),這相當有用。

自定義攔截器

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

 

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

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

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

以下例子演示通過(guò)繼承AbstractInterceptor,實(shí)現授權攔截器。

首先,創(chuàng )建授權攔截器類(lèi)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;
       }
       
   }


}

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

package tutorial;

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

接著(zhù),創(chuàng )建Action類(lèi)tutorial.AuthorizatedAccess模擬訪(fǎng)問(wèn)受限資源,它作用就是通過(guò)實(shí)現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;
   }

}

接下來(lái),新建Login.jsp實(shí)例化tutorial.Roles,并將其roles屬性賦予<s:radio>標志,代碼如下:

<% @ 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類(lèi)tutorial.Login將role放到session中,并轉到Action類(lèi)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文件,內容如下:

<! 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 >

發(fā)布運行應用程序,在瀏覽器地址欄中輸入:http://localhost:8080/Struts2_Interceptor/AuthorizatedAccess.action。由于此時(shí),session還沒(méi)有鍵為“ROLE”的值,所以返回Login.jsp頁(yè)面,如圖2所示:


圖2 Login.jsp

選中Employee,點(diǎn)擊Submit,出現圖3所示頁(yè)面:


圖3 ShowRole.jsp

總結

攔截器是Struts 2比較重要的一個(gè)功能。通過(guò)正確地使用攔截器,我們可以編寫(xiě)高可復用的代碼。

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
struts2.x中的攔截器
Struts2防止表單重復提交
Struts2教程9:實(shí)現自已的攔截器
Struts2攔截器和監聽(tīng)器
Struts2的攔截器機制
struts-defaut包的定義
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久