鐵手劍譜

          上善若水
          數(shù)據(jù)加載中……
          Struts秘籍之第1段:第2.8式. 有選擇地禁止Action

          第2.8式. 有選擇地禁止Action

          問題

          你想要是使用一個(gè)定制屬性來禁止(disable)一個(gè),并且該屬性能夠在struts-config.xml文件的action元素中進(jìn)行設(shè)置;轉(zhuǎn)發(fā)到該disabled action 的任何請(qǐng)求都會(huì)得到"under construction" 頁面。

          動(dòng)作要領(lǐng)

          創(chuàng)建一個(gè)定制的ActionMapping擴(kuò)展(如Example 2-16) ,它可以提供一個(gè)boolean 類型的屬性來指示action 是否被禁止。

          Example 2-16. 定制ActionMapping

           

          import org.apache.struts.action.ActionMapping;

          public class DisablingActionMapping extends ActionMapping {

              
          private String disabled;
              
          private boolean actionDisabled = false;
              
              
          public String getDisabled( ) {
                  
          return disabled;
              }


              
          public void setDisabled(String disabled) {
                  
          this.disabled = disabled;
                  actionDisabled 
          = new Boolean(disabled).booleanValue( );
              }

              
              
          public boolean isActionDisabled( ) {
                  
          return actionDisabled;
              }

          }

          這個(gè)action mapping 就可以在struts-config.xml文件中指定。如果你想要一個(gè)action被禁止,你可以設(shè)置disabled屬性為True :

          <action-mappings type="com.oreilly.strutsckbk.DisablingActionMapping">

            
          <!-- Edit mail subscription -->
            
          <action    path="/editSubscription"
                       type
          ="org.apache.struts.webapp.example.EditSubscriptionAction"
                  attribute
          ="subscriptionForm"
                      scope
          ="request"
                   validate
          ="false">
              
          <set-property property="disabled" value="true"/>
              
          <forward name="failure"              path="/mainMenu.jsp"/>
              
          <forward name="success"              path="/subscription.jsp"/>
            
          </action>

          然后創(chuàng)建一個(gè)定制的RequestProcessor,比如Example 2-17中的那個(gè),它可以處理DisablingActionMapping.

          Example 2-17. 處理對(duì)被禁止的actions的請(qǐng)求

           

          import java.io.IOException;

          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          import org.apache.struts.action.Action;
          import org.apache.struts.action.ActionForm;
          import org.apache.struts.action.ActionForward;
          import org.apache.struts.action.ActionMapping;
          import org.apache.struts.action.RequestProcessor;

          public class CustomRequestProcessor extends RequestProcessor {
              
              
          protected ActionForward processActionPerform(HttpServletRequest request, 
                     HttpServletResponse response, Action action,ActionForm form, 
                      ActionMapping mapping) throws IOException, ServletException 
          {
                  ActionForward forward 
          = null;
                  
          if (!(mapping instanceof DisablingActionMapping)) {
                      forward 
          = super.processActionPerform( request, response, 
                                                              action, form, mapping);
                  }

                  
          else {
                      DisablingActionMapping customMapping 
          = 
                            (DisablingActionMapping) mapping;
                      
          if (customMapping.isActionDisabled( )) {
                          forward 
          = customMapping.findForward("underConstruction");
                      }

                      
          else {
                          forward 
          = super.processActionPerform( request, response,
                                                                   action, form, mapping);
                      }

                  }

                  
          return forward;
              }

          }

          動(dòng)作變化

          Struts 通過兩種機(jī)制來對(duì)action提供定制屬性的能力。

          首先,每個(gè)Struts action 都可以通過一個(gè)通用參數(shù)parameter值來傳遞:


          <action    path="/editRegistration"
                     type
          ="org.apache.struts.webapp.example.EditRegistrationAction"
                attribute
          ="registrationForm"
                    scope
          ="request"
                 validate
          ="false"
                 parameter
          ="disabled">
            
          <forward name="success" path="/registration.jsp"/>
          </action>

          其次,在Action的實(shí)現(xiàn)中,parameter的值可以通過下面的代碼來訪問:

          String parameterValue = mapping.getParameter(  );

          然而,某些Struts所提供的子類,比如DispatchAction 已經(jīng)使用了parameter屬性。因?yàn)槟阒豢梢灾付ㄒ粋€(gè)parameter屬性,所以,如果你使用這些預(yù)定義的Action子類,便不能再將parameter用作定制屬性值。

          對(duì)于完整的擴(kuò)展,你可以擴(kuò)展ActionMapping類,可選地為你所選擇的定制屬性提供accessor 和 mutator :

           

          package com.oreilly.strutsckbk;

          import org.apache.struts.ActionMapping

          public class MyCustomActionMapping extends ActionMapping {
              
          private String customValue;
              
          public String getCustomValue( ) return customValue; }
              
          public String setCustomValue(String s) { customValue = s; }
          }


          你可以在struts-config.xml文件中引用這個(gè)擴(kuò)展。如果定制action mapping 將被用于所有action,請(qǐng)將action-mappings元素的type屬性設(shè)置為定制擴(kuò)展的全限定類名:

           

          <action-mappings type="com.oreilly.strutsckbk.MyCustomActionMapping">


          否則,為定制action mapping所需的action元素設(shè)置className屬性。這兩種情況下,set-property元素都可以用來針對(duì)特定的action元素為定制擴(kuò)展中的JavaBean 屬性設(shè)置值:

          <action    path="/someAction"
                     type
          ="com.oreilly.strutsckbk.SomeAction"
                className
          ="com.oreilly.strutsckbk.MyCustomActionMapping">
            
          <set-property property="customValue" value="some value"/>
          </action>

          這種方案使用一個(gè)定制的RequestProcessor來處理定制ActionMapping的disabled 屬性。如果你對(duì)特定的action使用定制的ActionMapping,你可以在Action.execute()訪法中直接訪問定值A(chǔ)ctionMapping的屬性:

          boolean disabled = ((DisablingActionMapping) mapping).isActionDisabled(  );
          if (disabled) return mapping.findForward("underConstruction");

          相關(guān)招式

          你也可以使用授權(quán)(authorization) servlet 過濾器來解決這個(gè)問題。那是第11.8式的動(dòng)作。

          posted on 2005-05-19 13:07 鐵手 閱讀(1789) 評(píng)論(1)  編輯  收藏 所屬分類: JavaStruts系列

          評(píng)論

          # Struts 秘籍(CookBook)[TrackBack] 2005-11-12 18:29 阿泠

          本系列源改編自O(shè)'Reily的Strus Cookbook
          [引用提示]阿泠引用了該文章, 地址: http://blog.donews.com/inclear/archive/2005/11/12/624363.aspx
          主站蜘蛛池模板: 湘乡市| 乌鲁木齐县| 渭源县| 水城县| 连平县| 台前县| 杂多县| 彰化市| 永城市| 叶城县| 泽普县| 湟中县| 全椒县| 丰镇市| 鄂托克旗| 新绛县| 锡林浩特市| 林州市| 太湖县| 开平市| 滁州市| 丰台区| 乡城县| 滨海县| 沁源县| 得荣县| 崇仁县| 华坪县| 栖霞市| 铜山县| 阳信县| 前郭尔| 同江市| 纳雍县| 抚宁县| 武乡县| 霸州市| 仙桃市| 遵义县| 泸州市| 黄龙县|