2006年7月9日

          Struts 中的 html:options(摘抄)

          html:options是Struts中比較復(fù)雜的一個(gè)tage lib,用法靈活,但是Sturts提供的源碼exercise taglib中沒(méi)有提出常用jsp+ActionForm這樣形式的最直接的總結(jié),現(xiàn)從中總結(jié)如下,分兩種情況:數(shù)組和Collection。
            
            需求,要達(dá)到:
            <select name="beanCollectionSelect" multiple="multiple" size="10">
            <option value="value 0">Label 0</option>
            <option value="value 1" selected="selected">Label 1</option>
            <option value="value 2">Label 2</option>
            <option value="value 3" selected="selected">Label 3</option>
            <option value="value 4">Label 4</option>
            <option value="value 5" selected="selected">Label 5</option>
            <option value="value 6">Label 6</option>
            <option value="value 7">Label 7</option>
            <option value="value 8">Label 8</option>
            <option value="value 9">Label 9</option></select>
            
            要實(shí)現(xiàn)上述效果,需要兩步:
            第一:設(shè)置ActionForm,
            也分兩小步:第一小步必須在A(yíng)ctionForm中,有一句
            private Collection beanCollection;
            public Collection getBeanCollection();
            
            Collection beanCollection要確保是一個(gè)實(shí)現(xiàn),如ArrayList,如果不是則會(huì)報(bào)No collection found的錯(cuò)誤,Struts的最大不方便就是一旦出問(wèn)題,定位很難,不知道什么地方使用錯(cuò)誤,或忘記設(shè)置什么了。
            
            因?yàn)榍懊嫘枨笾衞ption的value值和label值不一樣,那么在beanCollection中保存的就是一個(gè)value和label組成的對(duì)象,名為L(zhǎng)abelvalueBean,在LabelvalueBean中有兩個(gè)屬性value和label,
            
            在程序某個(gè)地方要為beanCollection賦值,如:
            
            Vector entries = new Vector(10); 
            entries.add(new LabelvalueBean("Label 0", "value 0"));     
            entries.add(new LabelvalueBean("Label 1", "value 1"));     
            entries.add(new LabelvalueBean("Label 2", "value 2"));     
            entries.add(new LabelvalueBean("Label 3", "value 3"));     
            entries.add(new LabelvalueBean("Label 4", "value 4"));      
            entries.add(new LabelvalueBean("Label 5", "value 5"));     
             entries.add(new LabelvalueBean("Label 6", "value 6"));      
            entries.add(new LabelvalueBean("Label 7", "value 7"));      
            entries.add(new LabelvalueBean("Label 8", "value 8"));      
            entries.add(new LabelvalueBean("Label 9", "value 9"));
            
            然后執(zhí)行setBeanCollection(entries);
            這樣ActionForm中的beanCollection算有值了。
            第二小步,需要設(shè)置Selected,selected有兩種,單選和多選:
            在A(yíng)ctionForm中必須有:
            
            private String singleSelect = "Single 5"; 
            public String getSingleSelect()
             {
               return (this.singleSelect);
              } 
            public void setSingleSelect(String singleSelect)
             {
               this.singleSelect = singleSelect;
              }
            
            或多選,多選必須是數(shù)組:
            
            private String[] beanCollectionSelect = { "value 1", "value 3",
                                   "value 5" }; 
            public String[] getBeanCollectionSelect() {
              return (this.beanCollectionSelect);  }
              public void setBeanCollectionSelect(String beanCollectionSelect[])
             {
                this.beanCollectionSelect = beanCollectionSelect;
              }
            
            第二:在Jsp中寫(xiě)入tang lib語(yǔ)句如下:
            
            <html:select property="beanCollectionSelect" size="10" multiple="true">
                <html:optionsCollection name="testbean" property="beanCollection"/>  
             </html:select>
            
            其中testbean是ActionForm的名稱(chēng)。
            
            以上是html:options的Collection解決方案,如果option值很少,簡(jiǎn)單地可以實(shí)現(xiàn)為數(shù)組,兩步:
            第一:在A(yíng)ctionForm中,
            
            private String values[] =
               { "Magazine", "Journal", "News Paper","Other" }; 
            private String labels[] =
               { "L-Magazine", "L-Journal", "L-News Paper","L-Other"};
              private String selected = "Magazine";  
            public String getSelected()
            {
               return selected;
              }  
            public void setSelected(String selected)
            {
               this.selected = selected;
              } 
            public String[] getvalues()
            {
               return values;
              }  
            public void setvalues(String[] values)
            {   this.values = values;
              } 
            public String[] getLabels()
            {
               return values;
              }  
            public void setLabels(String[] labels)
            {
               this.labels = labels;
              }
            
            第二步在jsp中:
            
            <html:select property="selected" >     
            <html:options name="testbean" property="values" labelProperty="label"/>   </html:select>
            
            Struts標(biāo)簽庫(kù)的使用還是需要小心,不必完全使用Struts的標(biāo)簽庫(kù),個(gè)人感覺(jué)Struts這種替代Html語(yǔ)句的標(biāo)簽庫(kù)有一種牽強(qiáng)附會(huì),給使用者掌握帶來(lái)難度,使用者除熟悉html外,還必須理解Struts的對(duì)應(yīng)標(biāo)簽庫(kù)用法,而且這種調(diào)試出錯(cuò),問(wèn)題也無(wú)法準(zhǔn)確定位,總是抽象地告訴你,no bean 或no form
          posted @ 2006-07-09 00:58 Redish 閱讀(819) | 評(píng)論 (1)編輯 收藏
          主站蜘蛛池模板: 达州市| 虎林市| 大姚县| 健康| 兴业县| 方城县| 偏关县| 建始县| 天气| 额济纳旗| 枣阳市| 安塞县| 鄯善县| 吉木乃县| 崇礼县| 扎兰屯市| 深州市| 长子县| 东光县| 石渠县| 聂荣县| 施甸县| 福贡县| 台山市| 新巴尔虎右旗| 永嘉县| 宾川县| 奉贤区| 洛浦县| 明光市| 昌都县| 乌海市| 黄冈市| 平安县| 涞源县| 金山区| 永康市| 巨野县| 汤阴县| 高青县| 涟源市|