1.
html:select該標簽生成一個select元素。multiple屬性決定是否為多選。如果指定了multiple="true"則為多選,此時對應的屬性應該是一個數組。否則,此時對應的屬性應該是標量。
注意:為了正確的處理未作選擇的情況,在ActionForm中的reset()方法中必須將標量屬性設置為默認值而將數組的長度置為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屬性方式的舉例如下:用數組的形式 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)實現舉例
?? 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;? //Collection 可以用List或者Vector
public Collection getBeanCollection();
Collection beanCollection要確保是一個實現,如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"));
然后執行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中:
//下面這個我使用時,出錯,總是提示找布道labels,不知什么原因?
<html:select property="selected" >
<html:options name="testbean" property="values" labelProperty="labels"/>
</html:select>
總結: 經過這段時間的開發和學習,自我感覺struts還是不錯的,雖然一開始的時候有點麻煩,
不過其實Java的配置也是有點麻煩的,不過用了這么長時間的Java,現在覺得Java挺好的!