posts - 32,  comments - 149,  trackbacks - 0
          1. html:select

          該標簽生成一個select元素。multiple屬性決定是否為多選。如果指定了multiple="true"則為多選,此時對應的屬性應該是一個數(shù)組。否則,此時對應的屬性應該是標量。

          注意:為了正確的處理未作選擇的情況,在ActionForm中的reset()方法中必須將標量屬性設置為默認值而將數(shù)組的長度置為0。?另外的一個重要問題就是struts如何生成option元素了,這個任務struts交給了html:option、html:options和html:optionsCollection三個標簽。

          ??? 1)html:option

          ???? 該標簽生成一個HTML的option元素。該標簽必須嵌在html:select標簽中。它的顯示文本來自其標簽體,也可以來自于資源文件。

          eg. <html:option value="red">紅色</html:option>????

          ???? <html:option value="blue">藍色</html:option>

          ?? 2)html:options

          ???? 該標簽生成多個HTML的option元素。該標簽必須嵌在html:select標簽中。

          ???? 指定collection屬性的方式舉例如下:

            <html:select name="userForm" property="uid" size="1">  

          <html:options collection="userCollection" property="uid" labelProperty="uname"/>

          </html:select>

          ??? 未指定collection屬性方式的舉例如下:用數(shù)組的形式 String uids[] ,String unames[]

            <html:select name="userForm" property="uid" size="1">  

          <html:options property="uids" labelProperty="unames"/>

          </html:select>

          ??? 3)html:optionsCollection標簽

          ???? 該標簽生成多個HTML的option元素。其功能和html:options標簽的相同。
          ????? userForm 里有個List userlist 它通過這樣得到的?
          ???? userlist.add(new LabelValueBean("label1","value1"));

          ? <html:select name="userForm" property="uid" size="1">????
          ? <html:optionsCollection name="userForm" property="userlist">?
          ? </html:select>

          ?? 4)實現(xiàn)舉例

          ?? html:options是Struts中比較復雜的一個tage lib,用法靈活,但是Sturts提供的源碼exercise taglib中

          ?? 沒有提出常用jsp+ActionForm這樣形式的最直接的總結,現(xiàn)從中總結如下,分兩種情況:數(shù)組

          和Collection。
            
            需求,要達到:
            <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>
            
            要實現(xiàn)上述效果,需要兩步:
            第一:設置ActionForm,
            也分兩小步:第一小步必須在ActionForm中,有一句
            private Collection beanCollection;? //Collection 可以用List或者Vector
            public Collection getBeanCollection();
            Collection beanCollection要確保是一個實現(xiàn),如ArrayList,
          ?? 如果不是則會報No collection found的錯誤,Struts的最大不方便就是一旦出問題,

          ?? 定位很難,不知道什么地方使用錯誤,或忘記設置什么了,不過可以查看tomcat Log的。
            
            因為前面需求中option的value值和label值不一樣,那么在beanCollection中保存的

          就是一個value和label組成的對象,名為LabelvalueBean,在LabelvalueBean中有兩個屬性value和label,
            
            在程序某個地方要為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算有值了。
            第二小步,需要設置Selected,selected有兩種,單選和多選:
            在ActionForm中必須有:
            
            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中寫入tang lib語句如下:
            
            <html:select property="beanCollectionSelect" size="10" multiple="true">
                <html:optionsCollection name="testbean" property="beanCollection"/>  
             </html:select>
            
            其中testbean是ActionForm的名稱。
            
            以上是html:options的Collection解決方案,如果option值很少,簡單地可以實現(xiàn)為數(shù)組,兩步:
            第一:在ActionForm中,
            
            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中:
            //下面這個我使用時,出錯,總是提示找布道labels,不知什么原因?
            <html:select property="selected" >     
            <html:options name="testbean" property="values" labelProperty="labels"/> 
            </html:select>
          總結: 經(jīng)過這段時間的開發(fā)和學習,自我感覺struts還是不錯的,雖然一開始的時候有點麻煩,

          不過其實Java的配置也是有點麻煩的,不過用了這么長時間的Java,現(xiàn)在覺得Java挺好的!

          posted on 2007-01-18 09:05 chunkyo 閱讀(4534) 評論(3)  編輯  收藏 所屬分類: openSource(struts&hibernate&spring等等)

          FeedBack:
          # re: Struts 中 html:options 的使用
          2008-05-07 19:03 | redleaf
          您好,這個有源碼嗎?
          沒怎么看懂  回復  更多評論
            
          # re: Struts 中 html:options 的使用 [未登錄]
          2008-07-24 16:04 | haha
          labelProperty屬性去掉就可以了,  回復  更多評論
            
          # re: Struts 中 html:options 的使用
          2009-04-01 17:56 | 打假
          去死吧,全他是抄來,且是錯的。  回復  更多評論
            
          <2007年1月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          這個博客主要是關于java技術和開源技術,大家一起來進步了!

          常用鏈接

          留言簿(12)

          隨筆分類

          隨筆檔案

          文章分類

          收藏夾

          DotNet

          Java技術網(wǎng)站

          Linux VS Unix

          其他常去網(wǎng)站

          常光顧的BLOG

          文學類網(wǎng)站

          游戲類網(wǎng)站

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 196859
          • 排名 - 293

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 宣威市| 扶绥县| 德格县| 忻城县| 绍兴市| 康保县| 长治市| 三明市| 莱州市| 灵璧县| 大丰市| 凌源市| 芜湖市| 铜川市| 淄博市| 子洲县| 津市市| 沙雅县| 海盐县| 双柏县| 杭锦后旗| 砀山县| 汶川县| 凌源市| 凤山县| 乌兰县| 德惠市| 武汉市| 都匀市| 刚察县| 都兰县| 阜城县| 沧州市| 奉化市| 永吉县| 富川| 渝中区| 永福县| 射阳县| 西乌珠穆沁旗| 连山|