html:select
html:select标签生成一个select元素。是单选还是多选取决于该标签的multiple属性。如果指定了multiple="true"则为多选,此时对应的属性应该是一个数组。如果没有指定multiple="true"则为单选,此时对应的属性应该是标量。
注意:为了正确的处理没有做任何的选择的情况,在ActionForm中的reset()方法中必须将标量属性设置为默认值而将数组的长度置为0。
另外的一个重要问题就是struts如何生成option元素了,这个任务struts交给了html:option、html:options和html:optionsCollection三个标签。
html:option标签
html:option标签生成一个HTML的option元素。该标签必须嵌在html:select标签中。它的显示文本来自其标签体,也可以来自于资源文件。它的value属性用来指定什么值将要被提交。
<html:option value="one">one</html:option> <html:option value="two">two</html:option>
html:options标签
html:options标签生成多个HTML的option元素。该标签必须嵌在html:select标签中。而且工作方式有些特殊,它的name与property属性和其它标签的name与property属性意义不一致,让我们具体看一下它的工作方式。
- 指定collection属性
- 没有指定collection属性
指定collection属性
让我通过示例来介绍在指定collection属性时该标签的工作方式,首先要说明selectForm中的persons和listForm中的persons完全一致。请参见bean:define标签。
下面的代码先利用bean:define标签将selectForm中的persons取到page作用域中,然后html:options标签再依据collection="personCollection"选中这个persons并将其中的每一个对象(Person类型)生成一个option元素。该标签的property="id"表示persons中的对象(Person类型)的id属性将作为option元素的value值。该标签的labelProperty="name"表示persons中的对象(Person类型)的name属性将作为option元素的label值。
当这个select提交时所选择的值将被提交到selectForm(name="selectForm")中的person对象(这是在SelectForm中声明的一个Person类型的域专门用来接收提交的值)的id属性中(property="person.id")。
<bean:define id="personCollection" name="selectForm" property="persons"/> <html:select name="selectForm" property="person.id" size="1"> <html:options collection="personCollection" property="id" labelProperty="name"/> </html:select>
没有指定collection属性
让我通过示例来介绍没有指定collection属性时该标签的工作方式,先来看看ids和names的定义:
private List<String> ids = null; private List<String> names = null;
上面的代码来自SelectForm,其中ids是一个String的列表,names也是一个String的列表。我们暂时假定这两个列表含有相同数目的元素。有了这些让我们开始介绍下面的代码。html:options标签用ids中的第i个值作为option元素的value值同时使用names中相同位置的值(第i个值)作为option元素的label值。如果ids比names长那么多出的ids中的值将即作为option的value又作为option的label。如果ids比names短那么多出的names的值会被丢掉。
当这个select提交时所选择的值将被提交到selectForm(name="selectForm")中的person对象(这是在SelectForm中声明的一个Person类型的域专门用来接收提交的值)的id属性中(property="person.id")。
<html:select name="selectForm" property="person.id" size="1"> <html:options property="ids" labelProperty="names"/> </html:select>
html:optionsCollection标签
html:optionsCollection标签生成多个HTML的option元素。该标签必须嵌在html:select标签中。它的功能和html:options标签的相同,但是它的name与property属性和其它标签的name与property属性意义一致,理解起来比较自然。
让我通过示例来介绍html:optionsCollection标签的用法。首先依据name="selectForm"和property="persons"取到selectForm中的persons列表,然后将列表中的对象(Person类型)的id属性作为option元素的value值(value="id"),将列表中的对象(Person类型)的name属性作为option元素的label值(label="name")。
<html:select name="selectForm" property="person.id" size="1"> <html:optionsCollection name="selectForm" property="persons" label="name" value="id"/> </html:select>
下面是一个多选的示例,虽然示例中使用了html:options标签,但是html:option和html:optionsCollection也能够用来多选。而且您还必须意识到html:option、html:options和html:optionsCollection这三个标签可以同时使用。代码中的personIds是SelectForm中声明的一个String[]类型的数组用来接收提交的多个值。
<html:select name="selectForm" property="personIds" multiple="true" size="2"> <html:options property="ids" labelProperty="names"/> </html:select>
--------------------------------------------------------------补充部分------------------------------------------------------
<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 ... />