Struts1:options和optionsCollection的使用及區別

Struts1系列中關於<select>標籤的選擇項<option>標籤的生成,提供了三種標籤,分別是<html:option>,<html:options>和<html:optionsCollection>。其中<html:option>用於生成單個的<option>標籤,而另外兩個則是用於生成一組<option>標籤。三個標籤可以同時出現在<html:select>的body中。

本文主要是記錄<html:options>和<html:optionsCollection>這兩個標籤的使用及區別。

 

 

術語知識

在我們下面的內容中,將會使用到labelvalue 兩個術語。下面是一個select的sample code,其中1和2表示一個option的value ,而Red和Blue則表示一個option的label ,是顯示給用戶看的東西。

<select name="selectName">

    <option value="1">Red</option>

    <option value="2">Blue</option>

</select>

 

 

 

<html:options>


支持的屬性有:collection,filter,labelName,labelProperty,name,property,style,styleClass

其中filter,style,styleClass都是一些通用的屬性,其他標籤也都有類似的屬性,功能相同,本文不再做解釋。真正需要注意的是其他的五個標籤。

 

collection表示任何scope中的一個collection對象的名字。根據collection是否被指定,該標籤有兩種使用方法。

 

collection被指定

如果collection被指定的話,那麼應該同時指定property和labelProperty屬性。其中property是collection對象中保存的對象中表示value的屬性名,labelProperty則是表示label的屬性名。如果labelProperty沒有被指定的話,那麼label將與value保持一致。

 

Struts框架已經爲我們提供了一個可以用於完成此項工作的util類:LabelValueBean,它有一個構造函數LabelValueBean(String label, String value)。當然,我們也可以使用自己創建的類。

下面的代碼就是我們使用這個LabelValueBean和<html:options>標籤的一段sample code。

<%

    List colorList = new ArrayList();

    colorList.add(new LabelValueBean("Red", "1");

    colorList.add(new LabelValueBean("Blue", "2");

%>

 

<html:select property="testProperty">

    <html:options collection="colorList" property="value" labelProperty="label"/>

</html:select>

 

 

collection未被指定

如果collection沒有被指定的話,那麼就要靠剩下的四個屬性了。(name,property,labelName,labelProperty)

其中name和property指向一個包含了<select>的value的collection對象。

如果只指定了name的話,表示任意scope中的一個collection對象的名字;

如果只指定了perperty的話,表示是ActionForm bean上的一個屬性的名字,它是一個collection對象;

如果兩者同時指定的話,name表示是任意scope中的一個對象,而property則是指這個對象上的一個屬性的名字,該屬性是一個collection對象。

 

而labelName和labelProperty是指向一個包含了<select>的label的collection對象。

如果兩者都不指定的話,則label和name和property所表示的value一致。

其餘三種情況和name和property的組合是類似的。

 

下面的代碼分別對value和label使用了不同的組合。

public ColorBean {

.........

    private String[] colorValue;

 

    public String[] getColorValue() {

        return this.colorValue;

    }

 

    public void setColorValue(String[] value) {

        this.colorValue = value;

    }

........

}

 

 

<%

    String[] colorLabel = {"Red", "Blue"};

 

    String[] tempColorValue = {"1", "2"};

    ColorBean colorBean = new ColorBean();

    colorBean.setColorValue(tempColorValue);

 

%>

 

<html:select property="testProperty">

    <html:options name="colorBean" property="colorValue" labelProperty="colorLabel"/>

</html:select>

 

 

一個小建議:

不要使用Map類型的對象,因爲我們在做應用程序的時候,<select>中的值一般都是用順序的,而Map類型則是無序的。

 

 

<html:optionsCollection>


支持的屬性有:filter,label,name,property,style,styleClass,value

其中filter,style,styleClass都是一些通用的屬性。

 

<html:optionsCollection>只有一種使用方式。其中name和property指定了一個collection類型的對象,而這個collection對象包含的bean對象同時包括了<select>的value和label。另外兩個label和value屬性則分別指代這個bean對象的label和value的屬性名。

 

其實,這個tag和類LabelValueBean算是珠聯璧合,因爲這個tag的label和value兩個屬性的默認值分別就是“label"和"value"。也就是說,如果使用的是LabelValueBean的話,我們甚至不需要指定label和value這兩個屬性,只需要指定name和property的組合就可以了。

 

閒話不多說,sample code奉上。

 

<%

    List colorList = new ArrayList();

    colorList.add(new LabelValueBean("Red", "1");

    colorList.add(new LabelValueBean("Blue", "2");

%>

 

<html:select property="testProperty">

    <html:optionsCollection name="colorList"/>

</html:select>

 

 

總結

1. <html:options>和<html:optionsCollection>都可以使用包含了類似於LabelValueBean的bean類的collection對象。但是,<html:options>只能使用存在於任意scope的collection對象,而<html:optionsCollection>不但可以使用存在於任意scope的collection對象,而且可以使用form上的collection對象,或者是存在於任意scope中的對象上的collection對象。

2. <html:options>是可以分別指定label和value的,一般指定到包含內容爲String類型的collection對象。

 

參考

<html:options>: http://struts.apache.org/1.x/struts-taglib/tagreference.html#html:options

<html:optionsCollection>: http://struts.apache.org/1.x/struts-taglib/tagreference.html#html:optionsCollection

 

 

發佈了72 篇原創文章 · 獲贊 10 · 訪問量 46萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章