<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    鐵手劍譜

    上善若水
    數(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 的任何請求都會得到"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. 處理對被禁止的actions的請求

     

    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ī)制來對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用作定制屬性值。

    對于完整的擴(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,請將action-mappings元素的type屬性設(shè)置為定制擴(kuò)展的全限定類名:

     

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


    否則,為定制action mapping所需的action元素設(shè)置className屬性。這兩種情況下,set-property元素都可以用來針對特定的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 屬性。如果你對特定的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 鐵手 閱讀(1791) 評論(1)  編輯  收藏 所屬分類: JavaStruts系列

    評論

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

    本系列源改編自O(shè)'Reily的Strus Cookbook
    [引用提示]阿泠引用了該文章, 地址: http://blog.donews.com/inclear/archive/2005/11/12/624363.aspx
      回復(fù)  更多評論    
    主站蜘蛛池模板: 中国xxxxx高清免费看视频| 亚洲啪啪免费视频| 日韩精品免费在线视频| 亚洲毛片免费观看| 亚洲视频在线免费看| 国产亚洲视频在线播放大全| 中文成人久久久久影院免费观看| 免费看黄视频网站| 在线a亚洲v天堂网2019无码| 亚洲男人天堂2018av| 国产午夜精品久久久久免费视 | 97免费人妻无码视频| 亚洲麻豆精品国偷自产在线91| 久久精品国产亚洲AV无码麻豆| 疯狂做受xxxx高潮视频免费| 免费人成网站在线播放| 亚洲理论片中文字幕电影| 久久久精品国产亚洲成人满18免费网站| 波多野结衣免费在线观看| 亚洲小说图片视频| 午夜理伦剧场免费| 亚洲七七久久精品中文国产| 中文字幕免费人成乱码中国| 国产a不卡片精品免费观看| 亚洲一级毛片免费看| 污污网站免费观看| 亚洲国产韩国一区二区| 在线免费视频一区| 久久亚洲最大成人网4438| 每天更新的免费av片在线观看| 亚洲最大中文字幕无码网站| 最近免费最新高清中文字幕韩国| 国产亚洲AV夜间福利香蕉149| 国产精品免费一区二区三区四区| 亚洲av永久无码精品三区在线4 | 亚洲va中文字幕无码| 亚洲精品无码久久久久APP| 国产精品美女午夜爽爽爽免费| 亚洲男人的天堂在线| 午夜时刻免费入口| 国产在线观看免费视频软件|