blog-moon

          導(dǎo)航

          <2025年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          統(tǒng)計(jì)

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          Office

          Websphere

          數(shù)據(jù)庫

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          頁面checkbox數(shù)組與stuts1.1

           

          經(jīng)過試驗(yàn),對(duì)于logic:iterate標(biāo)簽中的property代表的屬性,既可以是存放LabelValueBean類型對(duì)象的數(shù)組,也可以是存放LabelVauleBean類型對(duì)象的List,以下詳解如何使用struts 中的htnl:multibox標(biāo)簽

          Struts Tip #7 - Use Multibox to manage checkboxes

          Many applications need to use a large number of checkboxes to track options or selected items. To help with this, Struts provides the multibox control. It's quite handy but a little tricky to understand at first.

          The multibox leverages the way HTML handles checkboxes. If the box is not checked, the browser does not submit a value for the control. If the box is checked, then the name of the control and its value are submitted. This behavior is the reason there is a reset() method on the ActionForm. Since the browser will never signal that a box has been un-checked, the only solution is to reset all the boxes, and then check the ones that are now present in the request.

          The multibox control is designed to use an array of Strings. Each element in the array represents a checked box. To check a box, add a String to the array with the box's value. To uncheck a box, remove the element from the array. (Sound familiar?)

          When passed a value, the multibox control scans the elements of its array to see if there is a match. If so, the box is checked. If not, the box is left unchecked. If the user checks the box and submits the form, the box's value will be included in the request. The controller will then add that box to the "checked" array. If a box is unchecked, nothing is submitted, and nothing is added to the array. If the ActionForm bean is kept in the session context, in between requests, the reset() method needs to reduce the array to zero length (but not null).

          In this example,

          <logic:iterate id="item" property="items">
            <html:multibox property="selectedItems">
             <bean:write name="item"/> 
            </html:multibox> 
             <bean:write name="item"/> 
          </logic:iterate>

          The labels for the individual checkboxes is in the items property. The list of selected items is in an array named selectedItems. Items that are not selected are not present in the selectedItems array. The multibox checks the selectedItems array for the current item. If it is present, it writes a checked checkbox. If not, it writes an unchecked checkbox.

          Given an ActionForm setup like this

          private String[] selectedItems = {}; 
          private String[] items = {"UPS","FedEx","Airborne"}; 
          public String[] getSelectedItems() { 
            return this.selectedItems; 

          public void setSelectedItems(String[] selectedItems) { 
            this.selectedItems = selectedItems; 
          }

          The markup in the example would generate three checkboxes, labeled UPS, FedEx, and Airborne.

          <input type="checkbox" name="selectedItems" value="UPS">UPS 
          <input type="checkbox" name="selectedItems" value="FedEx">FedEx 
          <input type="checkbox" name="selectedItems" value="AirBorne">AirBorne

          Initially, the selectedItems array would be empty. If UPS were checked and submitted, it would become the equivalent of

          private String[] selectedItems = {"UPS"};

          If UPS and Airborne were both checked, it would become the equivalent of

          private String[] selectedItems = {"UPS","Airborne"};

          And when the checkboxes are rendered, the appropriate elements are automagically checked by the multibox tag.

          <input type="checkbox" name="selectedItems" value="UPS" checked="checked">UPS 
          <input type="checkbox" name="selectedItems" value="FedEx">FedEx 
          <input type="checkbox" name="selectedItems" value="AirBorne" checked="checked">AirBorne

          To provide different sets of labels and values, the standard LabelValueBean class [org.apache.struts.util.LabelValueBean] (since 1.1) can be used with the multibox control.

          <logic:iterate id="item" property="items">
            <html:multibox property="selectedItems"> 
              <bean:write name="item" property="value"/> 
            </html:multibox> 
              <bean:write name="item" property="label"/> 
          </logic:iterate>

          HTH - Ted.

          posted on 2006-06-25 16:54 moon 閱讀(841) 評(píng)論(0)  編輯  收藏 所屬分類: Web


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 永仁县| 巴马| 临洮县| 焉耆| 西畴县| 仲巴县| 四子王旗| 景宁| 府谷县| 寿阳县| 滕州市| 新干县| 新化县| 成安县| 兴和县| 巴彦淖尔市| 孟州市| 涿鹿县| 乐亭县| 花莲县| 镇康县| 六盘水市| 色达县| 洛宁县| 丽水市| 永登县| 建瓯市| 沙河市| 鹤峰县| 游戏| 师宗县| 桃园县| 嘉祥县| 宣威市| 津南区| 法库县| 广州市| 湖南省| 广汉市| 南华县| 剑川县|