打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
关于<html:options collection="" />的使用

关于<html:options collection="" />的使用

<html:select property="" >
<html:option value="">请选择...</html:option>
<html:options collection="${colname}" property="id" labelProperty="idValue" />
</html:select>

你的colname应该用表达式取值吧!这样的话好像不可以的!
用下EL表达式!

Struts中的下拉列表标签的使用
页面中经常用到下拉列表,下面是个人对于STRUTS中标签使用的一点总结:
STRUTS中的下拉选择列表标签必须嵌套在<html:form>标签中,包括:
1.<html:select>
2.<html:option>
3.<html:options>
4.<html:optionsCollection>

使用时嵌套如下:
<html:select property="ationForm.property">
<html:option>或<html:options>或<html:optionsCollection>
</html:select>
其中property为ActionForm中对应的一个属性.

1.<html:option>
<html:option value="value">displayName</html:option>
其中value为实际使用的值(赋值到ActionForm对应的属性中) displayName页面中显示的信息.
例:<html:option value=""></html:option>显示一个空白选择,值为"".

2..<html:options>
<html:options collection="collection" labelProperty="displayName" property="value"/>
其中collection为一个集合,一般是个ArrayList,displayName为前台显示的名称,value为后台实际使用的值.
例:<html:options collection="arrayList" labelProperty="name" property="id" />

3..<html:optionsCollection>
<html:optionsCollection property="actionForm.property" label="displayName" value="value"/>
其中property为ActionForm中的一个属性,为一个集合.displayName为前台显示的名称,value为后台实际使用的值.
例:<html:optionsCollection property="listProperty" label="name" value="id" />

补充一点:如果要从 数据库去取数据,一般是在 action 里调用 DAO ,把结果存入一个ArrayList作为 request 的一个属性传到页面上; 这时一般用 <html:options .../> 标签.另外,如果数据不从数据库去取,而是代码固定的,则一般把这种放到 ActionForm 里,作为属性在页面上取,这时一般用 <html:optionsCollection ... />
 

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>

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Struts 中 html:options 的使用
web中下拉列表的几种实现
Struts 1.x | <html> 标签库 - ray - JavaEye技术网站
欢迎光临 - 琳婕小筑-老猫的理想 - Struts配置说明 -
Struts1.x系列教程(5):HTML标签库
Struts标签(大全)二
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服