国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Struts中html:options的使用

2019-11-18 15:11:34
字體:
來源:轉載
供稿:網友

  Html:options是Struts中比較復雜的一個tage lib,用法靈活,但是Sturts提供的源碼exercise taglib中沒有提出常用jsp+ActionForm這樣形式的最直接的總結,現從中總結如下,分兩種情況:數組和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>

要實現上述效果,需要兩步:
第一:設置ActionForm,
也分兩小步:第一小步必須在ActionForm中,有一句
PRivate Collection beanCollection;
public Collection getBeanCollection();

Collection beanCollection要確保是一個實現,如ArrayList,假如不是則會報No collection found的錯誤,Struts的最大不方便就是一旦出問題,定位很難,不知道什么地方使用錯誤,或忘記設置什么了。

因為前面需求中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"));

然后執行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;    }

或多選,多選必須是數組:

 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值很少,簡單地可以實現為數組,兩步:
第一:在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中:

     <html:select property="selected" >            <html:options name="testbean" property="values" labelProperty="label"/>      </html:select>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 奇台县| 平武县| 旺苍县| 开阳县| 海兴县| 金湖县| 宁南县| 通河县| 平顶山市| 皋兰县| 株洲县| 东乡县| 兰西县| 叶城县| 桑植县| 腾冲县| 喀喇沁旗| 兴海县| 广宁县| 侯马市| 淳安县| 高淳县| 大余县| 荆州市| 怀宁县| 上饶市| 元氏县| 通辽市| 天台县| 尉犁县| 新泰市| 竹溪县| 舟山市| 江安县| 同江市| 安仁县| 集贤县| 新密市| 宜春市| 焉耆| 延边|