隨筆-34  評論-1965  文章-0  trackbacks-0

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

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

          什么是攔截器

          攔截器,在AOP(Aspect-Oriented Programming)中用于在某個方法或字段被訪問之前,進行攔截然后在之前或之后加入某些操作。攔截是AOP的一種實現策略。

          在Webwork的中文文檔的解釋為——攔截器是動態攔截Action調用的對象。它提供了一種機制可以使開發者可以定義在一個action執行的前后執行的代碼,也可以在一個action執行前阻止其執行。同時也是提供了一種可以提取action中可重用的部分的方式。

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

          實現原理

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

          圖1 攔截器調用序列圖
          圖1 攔截器調用序列圖

          已有的攔截器

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

          在本文使用是Struts 2的最新發布版本2.0.1。需要下載的朋友請點擊以下鏈接:
          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中已經配置了以上的攔截器。如果您想要使用上述攔截器,只需要在應用程序struts.xml文件中通過“<include file="struts-default.xml" />”將struts-default.xml文件包含進來,并繼承其中的struts-default包(package),最后在定義Action時,使用“<interceptor-ref name="xx" />”引用攔截器或攔截器棧(interceptor stack)。一旦您繼承了struts-default包(package),所有Action都會調用攔截器棧 ——defaultStack。當然,在Action配置中加入“<interceptor-ref name="xx" />”可以覆蓋defaultStack。

          下面是關于攔截器timer使用的例子。首先,新建Action類tuotrial/TimerInterceptorAction.java,內容如下:

          package tutorial;

          import com.opensymphony.xwork2.ActionSupport;

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

                 
          return SUCCESS;
             }

          }

          配置Action,名為Timer,配置文件如下:

          <! 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可以隨意寫些什么到里面。發布運行應用程序,在瀏覽器的地址欄鍵入http://localhost:8080/Struts2_Interceptor/Timer.action,在出現Timer.jsp頁面后,查看服務器的后臺輸出。

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

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

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

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

          自定義攔截器

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

           

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

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

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

          以下例子演示通過繼承AbstractInterceptor,實現授權攔截器。

          首先,創建授權攔截器類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;
                 }
                 
             }


          }

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

          package tutorial;

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

          接著,創建Action類tutorial.AuthorizatedAccess模擬訪問受限資源,它作用就是通過實現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 >

          然后,創建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>標志,代碼如下:

          <% @ 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 >

          創建Action類tutorial.Login將role放到session中,并轉到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文件,內容如下:

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

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

          圖2 Login.jsp
          圖2 Login.jsp

          選中Employee,點擊Submit,出現圖3所示頁面:

          圖3 ShowRole.jsp
          圖3 ShowRole.jsp

          總結

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

          posted on 2006-12-06 20:10 Max 閱讀(111584) 評論(72)  編輯  收藏 所屬分類: Struts 2.0系列

          評論:
          # re: Struts 2的基石——攔截器(Interceptor) 2006-12-06 20:43 | king[匿名]
          一直 在關注你struts2的文章,寫的很不錯。謝謝!!  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2006-12-06 22:50 | chwen[匿名]
          寫的不錯,通俗易懂!  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2006-12-07 09:31 | Tendy
          to Max:

          -------
          由于我們項目中的幾個資深員工除了Struts 1.x外,對其它的WEB框架似乎不大感興趣。
          -------

          商業軟件,
          選擇什么框架,
          其實他們未必能做主......
            回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2006-12-07 11:01 | Max
          @Tendy
          這個項目我們是可以自由選取框架。因為只是DEMO,不管我們用什么框架(或不用框架),實現所有功能需求就可以了。
          小發一下牢騷:)  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2006-12-07 15:58 | lix
          有時間大家去讀讀Ted他們寫的struts2的文檔和example,會有更多認識,文檔寫得蠻清楚的  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2006-12-08 09:46 | 張先生
          2.0.1還是beta版,究竟啥時發布啊??等....  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2007-03-23 11:42 | yangdamao
          謝謝,正在學習中,感覺不錯!  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2007-04-16 17:43 | xianglg
          配置文件中的
          < result name ="success" > /ShowRole.jsp </ result >
          應該改為ShowUser.jsp  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2007-04-19 18:04 | xulao
          樓主,你覺得要快速開發一個WEB原型系統,搭什么樣的開發環境比較理想  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2007-04-19 22:47 | Max
          @xulao
          我個人認為MyEclipse 5.1或NetBean 5.5都不錯的。  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2007-04-25 09:17 | ddd
          偶用eclipse, MyEclipse不是需要注冊的嘛!

          大家都是買版權的嗎?

          關于這個權限攔截的例子, 是否應該在用戶鍵入任何頁面時,都調用,

          這樣的話,要如何實現呢?  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2007-04-25 14:02 | ddd
          LS的問題, 知道了,

          是否在每個action定義塊里面, 加上< interceptor-ref name ="auth" />
          就可以了呢?

          如果用戶直接指定的url不是action, 也是jsp的話,
          該如果調用攔截器呢?  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2007-05-21 22:30 | yesw
          <s:radio list ="#roles.roles" value ="EMPLOYEE" name ="role" label ="Role" /> 是不是設置單選按鈕的默認值是"EMPLOYEE"啊,為什么頁面上的Employee單選按鈕沒有被選中???  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2007-05-22 09:21 | Max
          @yesw
          用value ="'EMPLOYEE'",就可以了。  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2007-07-31 09:31 | wsc
          It good article! Thanks to Max  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor)[未登錄] 2007-08-11 15:56 | Steve
          HI!Max:
          你能不能寫一個關于ActionInvocation中addPreResultListener方法的例子(運用在攔截器中的)
          最好詳細點,我看文檔看的暈暈的!!
          先謝了啊  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2007-08-12 15:13 | louleigh
          MAX老大。
          這個程序我調不通啊

          首先是有一個duplicate s的錯誤。我估計是不是兩個jsp都用了
          prefix="s"的問題。.后來我把另外一個改成z就好了。
          這個問題我已經成功解決.

          另外有一個問題就是login.jsp中的
          <s:bean id="roles" name="tutorial.Roles">他報錯。他說attrbuite no alue.
          請問該怎么解決~  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2007-08-24 17:07 | jun jun
          Struts Problem Report

          Struts has detected an unhandled exception:
          # Messages: No result defined for action login.Login and result input
          File: file:/D:/PROJECTS/STRUTS/apache-tomcat-5.5.23/webapps/ROOT/WEB-INF/classes/strutsLogin.xml
          Line number: 72
          Column number: 44

          </action>



          <action name="Login" class="login.Login">

          <result name="SUCCESS" type="chain">AuthorizateAccess</result>

          </action>

          我的也是這么做的,為什么要報錯呢?  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor)[未登錄] 2007-08-29 15:40 | sclsch
          mark  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2007-09-26 11:18 | lasa
          謝謝樓主寫的這么好的文章。

          關于攔截器遇到一個問題。
          當我是使用struts2和spring2集成的時候,如果在action加入攔截器,
          從A頁面提交action 然后在轉到B頁面的數據就沒法得到了。
          <action name="HelloWorld" class="helloWorld">
          <interceptor-ref name="timer" />
          <result>HelloWorld.jsp</result>
          </action>
          不知道有沒有兄弟碰到類似的問題。  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2007-09-27 10:19 | torry
          誰能告訴我怎樣在攔截器得到action的name?謝謝  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2007-09-27 10:30 | torry
          不好意思,我找到那個函數了,是ActionInvocation.getInvocationContext().getName()可以得到action的name  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2007-10-14 14:26 | hpyzay
          ai.getAction()這個是什么意思啊,< s:radio list ="#roles.roles" value ="'EMPLOYEE'" name ="role" label ="Role" />其中list="#roles.roles"的后一個roles是什么啊 請各位指教  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2007-11-13 09:19 | rocketwang
          在interceptor中可以修改action的屬性值嗎?  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2007-11-13 16:20 | kinghlc
          @lasa
          我有碰到這種情況,一直沒找到解決的辦法,最后不得不放棄使用攔截器進行權限檢查  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2007-11-20 13:42 | 高山流水
          @hpyzay---
          Object getAction()
          Get the Action associated with this ActionInvocation
          -------------
          list="#roles.roles" == roles.getRoles();  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2007-12-03 16:26 | 弱弱
          弱弱的問下Login那個action里的session是從哪里傳進去的?  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2007-12-07 09:40 | kinghlc
          正確使用自定義攔截器的方法:定義好攔截器之后,在定義一個攔截器棧,并繼承默認的攔截器棧  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2007-12-21 21:49 | 技術交流
          樓主若對j2ee技術感興趣,我們誠邀您加入我們的技術討論QQ群!本群加入條件為1年以上java工作經驗! 41732384  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2008-01-11 14:58 | nb
          1  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor)[未登錄] 2008-01-17 10:56 | 西西
          老大,struts.xml里的< result name ="success" > /ShowRole.jsp </ result > 應該是ShowUser.jsp吧;一開始我也是復制的,后來看,這里有點小問題,改過來就好了  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2008-02-14 17:41 | jammth
          請問ai.invoke()執行后返回不到ShowUser.jsp頁面是什么原因呢?  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2008-03-10 14:50 | lastsweetop
          加入攔截器 Action中的execute就無法調用  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2008-03-24 09:35 | oh,no..
          不是吧,struts2和struts1.2差別這么大,現在最新的netbeans6.01也只支持struts1.2..  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2008-04-01 13:46 | guxx
          請問如何屏蔽freemarker.template.TemplateException: Error reading included file admin/views/contentAdm.ftl這個異常信息?  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2008-04-05 17:15 | 開始瘋狂
          @技術交流
          一個qq群搞的給公司招聘一樣,還工作經驗什么滴。  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2008-04-18 11:47 | ni ba
          @wsc
          sb  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2008-04-18 11:48 | ni ba
          @lastsweetop
          你sb阿  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2008-04-22 14:59 | billpan100
          讀完后,想頂一下! 寫得很好!   回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor)[未登錄] 2008-06-04 15:25 | John
          通俗易懂,又有所收獲了  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2008-06-10 11:24 | jiangjkd
          < result name ="login" > /Login.jsp </ result >
          < result name ="success" > /ShowRole.jsp </ result >

          為什么 name 屬性值是 login 和 success.這是在哪里規定的呢  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2008-06-30 10:13 | nuoting
          我想問一下:
          org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor這個攔截器的源代碼在哪里呢?或者它的class文件又在哪里呢?  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor)[未登錄] 2008-08-05 15:56 | matrix
          挺棒的文章,想學struts2,就來這看看了。  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2008-08-06 14:10 | 代理163
          通俗易懂  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2008-08-09 10:26 | ll
          大家都說好。其實也不錯。就是少了點。。。
          希望多點更底層更本質的struts2的東西、、、謝謝  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2008-08-11 12:34 | zff
          攔截器我是第一次用, 那個roles.java是怎么讀到的??  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2008-08-25 21:46 | jurnzhou
          workshop studio...開發struts1.x...不是一般強大...

          IDE易用度我覺得足以框架的在維護上的不足...  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2008-09-25 13:45 | 高舉
          多謝樓主貢獻  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2008-10-07 21:21 | bb
          好文章,我的布署成功了,但是有一個問題:
          第一次test:
          (1)訪問http://127.0.0.1:8081/struts2/Login.action,頁面出現正常(供角色選擇頁),然后選擇'Employee',頁面顯示"Your role is:EMPLOYEE"正常,
          (2):不關閉IE把,IE中的Login修改AuthorizatedAccess,我想應該頁面應該還是會顯示"Your role is:EMPLOYEE",可是出現了角色選擇頁,與我預見不同。
          why?

          第二次test:
          (1):半閉第一次test ie,輸入http://127.0.0.1:8081/struts2/AuthorizatedAccess.action,頁面進入角色選擇頁,這是對的,然后選擇'Employee',IE中變成http://127.0.0.1:8081/struts2/Login.action;jsessionid=796BA48C1D997979A39F53C9B665E513,頁面出現"Your role is:EMPLOYEE".正常
          (2):不關閉IE把,IE中的Login修改AuthorizatedAccess,頁面出現"Your role is:EMPLOYEE".正常。
          (3):可是只要我把;jsessionid=796BA48C1D997979A39F53C9B665E513去了,就又回到角色選擇頁了
          WHY?
          謝謝各位
            回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2008-10-11 00:02 | 畢達哥拉斯
          @lasa
          你加的攔截器覆蓋了缺省的攔截器,所以無法獲將提交上來的數據整理到Action中,你可以自己定義攔截器棧,其中包括default的攔截器棧,后面加上你自己的攔截器就ok了。  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2008-10-18 09:39 | bb
          謝謝您的回復,我剛學struts2,是個fresh man,您能不能貼上相關的代碼配置  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor)[未登錄] 2008-11-20 10:33 | 啊啊
          <result name ="login" > 和
          <action name ="Login" class ="tutorial.Login" >

          2各中的name我為什麼不能改,換一個名字就會出錯 為什麼一定是login呢 在哪裏能改呢 誰告訴我下

          Login.jsp裏面調action的login也改了
            回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor)[未登錄] 2008-11-29 10:14 | 初出茅廬
          @畢達哥拉斯
          你的方法是可行的
          可以自己定義自己的攔截器棧 將自己的攔截器和系統的攔截器都放在里面
          引用自己的攔截器棧就行了
          <interceptors>
          <!-- 定義自己的攔截器 -->
          <interceptor name="chk" class="com.langwei.roles.common.TestLogin"></interceptor>
          <!-- 定義默認的檢測器棧 -->
          <interceptor-stack name="mydefaultStack">
          <interceptor-ref name="defaultStack"/>
          <interceptor-ref name="chk"/>
          </interceptor-stack>
          </interceptors>   回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2008-12-09 15:54 | icewind
          文章很不錯,我正在慢慢學習中,只是有幾個例子調不通,Max可否吧所有的例子打包發給我,icewind5312@163.com,萬分感謝  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2008-12-18 08:12 | 匿名
          max說的很好,建議那些不懂的人先看看struts2再說。
            回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2009-05-23 15:24 | ly
          @kinghlc
          對頭~~~  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2009-05-23 15:24 | ly
          @初出茅廬
          關鍵啊~~~~  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2009-06-19 16:15 | min
          看了你寫的東西,挺好的,懂老,THANKS。  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2009-09-02 01:12 | hu ai
          @lasa你是不是用屬性傳參的。你使用攔截器后,會交黙認的攔截器覆蓋。  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor)[未登錄] 2010-05-14 09:25 | alin
          寫的真清楚!  回復  更多評論
            
          # 非常不錯[未登錄] 2010-11-14 16:16 |
          感謝Max 。時間飛快,我是從chm 中看到的,因為有的圖看不到,所以就進到網址來看。  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2010-12-08 11:20 | fdxganli
          @高山流水
            回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2010-12-17 14:36 | ap
          @Steve
          5293
          你可以看看搜索一struts2視頻,上面有。youku上找一下。  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2010-12-17 14:44 | ap
          @啊啊
          那個是固定的好像是。result有幾個默認值。比如 input success login估計也是,因為你看 那個攔截器那個java文件有一句 return Action.LOGIN; 有不對的地方請指出。我也剛學習不久。  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2011-02-09 10:39 |
          樓主很專業,小弟 很佩服!  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor)[未登錄] 2011-02-15 14:36 | jacky
          @畢達哥拉斯
          我按照你說的方法試過了,但還是無法獲得jsp頁面傳來的參數  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor)[未登錄] 2011-02-15 14:40 | jacky
          謝謝各位,終于搞定了,原來在action中要加入<interceptor-ref name="mydefaultStack"/>這句啊……
            回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2011-11-13 22:31 | 裂紋
          為什么我按照你寫的例子去配置攔截器,提示找不到文件呢?我配置了好幾個都不行,不曉得為什么求解


          type Status report

          message /Struts2_Interceptor/

          description The requested resource (/Struts2_Interceptor/) is not available.
            回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2014-03-01 11:26 | 狄仁杰
          寫的很不錯,看完之后就學會了利用攔截器。  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2014-03-01 11:31 | 狄仁杰
          @ddd
          可以配置全局攔截器
          <default-interceptor-ref name="auth" />
          <global-results>
          <result name="login">/Login.jsp</result>
          </global-results>  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2014-11-14 17:34 | osmond.gao
          為什么跳轉到點擊提交后跳轉到Login.action就報500異常了呢  回復  更多評論
            
          # re: Struts 2的基石——攔截器(Interceptor) 2014-11-16 17:56 | osmond.gao
          為什么我的Login類的兩個私有屬性一定要初始化才行,不初始化就報500錯誤
          我得寫成下面這樣才行
          public class Login extends ActionSupport implements SessionAware {
          private String role = "";//初始化值
          private Map<String, String> session = new Hashtable<String, String>();//實例化對象  回復  更多評論
            
          主站蜘蛛池模板: 双鸭山市| 英吉沙县| 泸西县| 巴林左旗| 郑州市| 六盘水市| 法库县| 长岭县| 柳州市| 新田县| 新乡县| 胶南市| 新泰市| 元江| 桑日县| 凌云县| 湖口县| 平山县| 丹巴县| 抚顺市| 平果县| 乌什县| 抚州市| 左权县| 青阳县| 安龙县| 黎城县| 磐安县| 沧源| 永春县| 黎平县| 融水| 霸州市| 上栗县| 吉水县| 玉山县| 舟山市| 福清市| 霸州市| 河北区| 普宁市|