1.
html:select該標(biāo)簽生成一個(gè)select元素。multiple屬性決定是否為多選。如果指定了multiple="true"則為多選,此時(shí)對(duì)應(yīng)的屬性應(yīng)該是一個(gè)數(shù)組。否則,此時(shí)對(duì)應(yīng)的屬性應(yīng)該是標(biāo)量。
注意:為了正確的處理未作選擇的情況,在ActionForm中的reset()方法中必須將標(biāo)量屬性設(shè)置為默認(rèn)值而將數(shù)組的長(zhǎng)度置為0。?另外的一個(gè)重要問(wèn)題就是struts如何生成option元素了,這個(gè)任務(wù)struts交給了html:option、html:options和html:optionsCollection三個(gè)標(biāo)簽。
??? 1)html:option
???? 該標(biāo)簽生成一個(gè)HTML的option元素。該標(biāo)簽必須嵌在html:select標(biāo)簽中。它的顯示文本來(lái)自其標(biāo)簽體,也可以來(lái)自于資源文件。
eg. <html:option value="red">紅色</html:option>????
???? <html:option value="blue">藍(lán)色</html:option>
?? 2)html:options
???? 該標(biāo)簽生成多個(gè)HTML的option元素。該標(biāo)簽必須嵌在html:select標(biāo)簽中。
???? 指定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標(biāo)簽
???? 該標(biāo)簽生成多個(gè)HTML的option元素。其功能和html:options標(biāo)簽的相同。
????? userForm 里有個(gè)List userlist 它通過(guò)這樣得到的?
???? userlist.add(new LabelValueBean("label1","value1"));
? <html:select name="userForm" property="uid" size="1">????
? <html:optionsCollection name="userForm" property="userlist">?
? </html:select>
?? 4)實(shí)現(xiàn)舉例
?? 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,
也分兩小步:第一小步必須在ActionForm中,有一句
private Collection beanCollection;? //Collection 可以用List或者Vector
public Collection getBeanCollection();
Collection beanCollection要確保是一個(gè)實(shí)現(xiàn),如ArrayList,
?? 如果不是則會(huì)報(bào)No collection found的錯(cuò)誤,Struts的最大不方便就是一旦出問(wèn)題,
?? 定位很難,不知道什么地方使用錯(cuò)誤,或忘記設(shè)置什么了,不過(guò)可以查看tomcat Log的。
因?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有兩種,單選和多選:
在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中寫(xiě)入tang lib語(yǔ)句如下:
<html:select property="beanCollectionSelect" size="10" multiple="true">
<html:optionsCollection name="testbean" property="beanCollection"/>
</html:select>
其中testbean是ActionForm的名稱。
以上是html:options的Collection解決方案,如果option值很少,簡(jiǎn)單地可以實(shí)現(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中:
//下面這個(gè)我使用時(shí),出錯(cuò),總是提示找布道labels,不知什么原因?
<html:select property="selected" >
<html:options name="testbean" property="values" labelProperty="labels"/>
</html:select>
總結(jié): 經(jīng)過(guò)這段時(shí)間的開(kāi)發(fā)和學(xué)習(xí),自我感覺(jué)struts還是不錯(cuò)的,雖然一開(kāi)始的時(shí)候有點(diǎn)麻煩,
不過(guò)其實(shí)Java的配置也是有點(diǎn)麻煩的,不過(guò)用了這么長(zhǎng)時(shí)間的Java,現(xiàn)在覺(jué)得Java挺好的!