size 指定每次在网页上显示的可选项的数目。
multipe 指定是否支持多项选择,如果设置为true,则支持多项选择。否则只支持单选,默认值false。
property 与ActionForm中的某个属性对应,这个属性用来存放用户在列表上选中选项的值。在单项选择情况下,ActionForm对应属性应该定义为简单类型(不能为数组);在多项选择情况下,ActionForm中的对应属性应该定义为数组类型,以便存放用户选择的多个选项。
value 指定下拉列表默认选中的项。
一般使用方法:
<html:select property="XXX">
<html:option>或<html:options>或<html:optionsCollection>
</html:select>
在<html:select>标签中可以包含多个<html:option>,<html:options>,<html:optionCollections>元素。
一、<html:option>标签生成HTML<option>元素,其value属性指定可选项的实际值。
1.直接指定
<html:option value="实际值">显示内容</html:option>
2.读取资源文件
例如:ApplicationResources.properties资源文件中存在如下键值对:a1=happySelect
标签中通过key关联到资源文件,指定要显示内容。
<html:option value="实际值" key="a1" /> 这样在页面上显示出happySelect
3.通过key、bundle同时指定要显示的内容
bundle与Struts配置文件中<message-resources>元素配置的Resource Bundle的资源文件key匹配
<message-resources parameter="com.struts.happy" key="happyhtml"/>
<html:option value="实际值" bundle="happyhtml" key="a1" /> 这样在页面上显示出happySelect
把列表的可选项的显示文本存放在资源文件中,而不是直接在JSP文件中指定,有利于实现国际化。
二、<html:options>标签生成一组HTML标签<option>元素
1.使用coolection属性指定存在某个范围中的集合来生成列表项,注意coolection属性指定的集合,该对象的每一个元素为一个Bean。
例如:
package com.pojo; public class Users { private String userName; private String userValue; public Users(){} public Users(String userName,String userValue) { this.userName=userName; this.userValue=userValue; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserValue() { return userValue; } public void setUserValue(String userValue) { this.userValue = userValue; } }
将实体类实例放入ArrayList列表然后放入request范围内
Users u1=new Users("1","高中"); Users u2=new Users("2","本科"); Users u3=new Users("3","硕士"); Users u4=new Users("4","博士"); ArrayList array=new ArrayList(); array.add(u1); array.add(u2); array.add(u3); array.add(u4); request.setAttribute("xueli",array);
使用<html:options>标签生成可选项
<html:select property="xueli" multiple="true" size="3"> <html:options collection="xueli" property="userName" labelProperty="userValue"/> </html:select>collection指定存放在request范围内的集合
property指定<html:option>实际值
labelProperty指定<html:option>显示到页面上的文本
当使用property属性和labelProperty属性时,会根据属性指定的名称调用相应Bean中的getXXX方法来获得属性值。
生成HTML效果如下
<option value="1">高中</option> <option value="2">本科</option> <option value="3">硕士</option> <option value="4">博士</option>
实现以上效果可以使用Struts框架特有的类LabelValueBean类,这样就省去了自己定义Bean的麻烦了:
ArrayList array=new ArrayList(); array.add(new LabelValueBean("1","高中")); array.add(new LabelValueBean("2","本科")); array.add(new LabelValueBean("3","硕士")); array.add(new LabelValueBean("4","博士")); request.setAttribute("xueli",array); <html:select property="xueli" multiple="true" size="3"> <html:options collection="xueli" property="value" labelProperty="label"/> </html:select>
另,未指定collection属性方式的举例如下:
<html:select name="userForm" property="uid" size="1"> <html:options property="uids" labelProperty="unames"/> </html:select>
其中uids和unames为等长的数组。
2.利用name属性指定存放在某个范围中的对象
例如
Object[] obj=new Object[]{"高中","本科","硕士","博士"}; request.setAttribute("xueli",array); <html:options name="xueli"/>
生成HTML效果如下:
<option value="高中">高中</option> <option value="本科">本科</option> <option value="硕士">硕士</option> <option value="博士">博士</option>
如果
<html:options name="xueli" property="userName" labelProperty="userValue"/>
将报如下异常
javax.servlet.jsp.JspException: No getter method available for property userName for bean under name xueli
三、<html:optionsCollection>标签与<html:options>标签相似
它通过name属性或property属性指定一个集合对象,该对象中的每一个元素为一个Bean,并且在Bean中分别具有与标签中label属性和value属性指定的值匹配的getXXX方法。
如果使用name属性,那么将存放在pageContext/request/session/application中的Bean作为指定的集合;如果使用property属性,那么将使用和表单相关联的FormBean中的属性作为指定的集合。
也可以同时使用二者,此时用法同其他html标签。
其中label属性用来指定列表项的标签(显示给指定的用户),value属性用来指定实际值(提交给服务器的值)
例如:
ArrayList列表然后放入reqeust范围内
Users u1=new Users("1","高中"); Users u2=new Users("2","本科"); Users u3=new Users("3","硕士"); Users u4=new Users("4","博士"); ArrayList array=new ArrayList(); array.add(u1); array.add(u2); array.add(u3); array.add(u4); request.setAttribute("xueli",array); <html:select> <html:optionsCollection name="xueli" value="userName" label="userValue"/> </html:select>
该标签和org.apache.struts.util.LabelValueBean结合得更好:
ArrayList array=new ArrayList(); array.add(new LabelValueBean("1","高中")); array.add(new LabelValueBean("2","本科")); array.add(new LabelValueBean("3","硕士")); array.add(new LabelValueBean("4","博士")); request.setAttribute("xueli",array); <html:select> <html:optionsCollection name="xueli"/> </html:select>