当前位置: 代码迷 >> HTML/CSS >> <html:select><html:option><html:options>标签
  详细解决方案

<html:select><html:option><html:options>标签

热度:996   发布时间:2012-09-16 17:33:16.0
<html:select>,<html:option>,<html:options>标签
在<html:select>标签中可以包含多个<html:option>,<html:options>,<html:optionCollections>元素。

<html:select>标签有以下重要属性

size属性:指定每次在网页上显示的可选项的数目。


multipe属性:指定是否支持多项选择,如果设置为true,就表示多选列表,支持多项选择。否则只表示下拉列表。只支持单选操作。默认值false

property属性:与ActionForm Bean中的某个属性对应,这个属性用来存放用户在列表上选中选项的值。在单项选择的情况下,ActionForm Bean中的对应属性对应该列表上选项的值。在单项选择情况下,

ActionForm Bean对应属性应该定义为简单类型(不能为数组),在多项选择情况下,ActionForm Bean中的对应属性应该定义为数组类型,以便存放用户选择的多个选项。

<html:option>


标签生成HTML<option>元素,这个标签被嵌套在<html:select>标签中,代表列表的一个可选项的Label,既现在是在页面上的值。这中值有两个来源。

1.直接指定文本内容
<html:option value="1">Label来源1:直接指定文本内容</html:option>

2.通过Resource Bundle中的内容。

例如:ApplicationResources.properties资源文件中存在如下键值对:a1=happySelect

标签中通过key关联到资源文件,指定要显示内容。
<html:option value="1" key="a1" /> 这样在页面上显示出happySelect

3.通过key,bundle同时指定要显示的内容

bundle与Struts配置文件中<message-resources>元素配置的Resource Bundle的资源文件key匹配

<message-resources parameter="com.struts.happy" key="happyhtml"/>

<html:option value="1" bundle="happyhtml" key="a1" /> 这样在页面上显示出happySelect


把列表的可选项的显示文本存放在Resource Bundle中,而不是直接在JSP文件中指定,有利于实现国际化。


<html:option>元素的value属性指定可选项的实际值。

<html:options>

标签生成一组HTML标签<option>元素。在<html:select>元素中可以包含多个<html:options>元素。


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列表然后放入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: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>


2.利用name属性指定存放在某个范围中的对象,并有property属性指定该对象的某个属性来生成列表项。

例如
   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

如果使用name属性指定集合那么需要使用<html:optionsCollection >标签

<html:optionsCollection>

<html:select>标签中可以嵌套多个<html:optionsCollection>标签


标签与<html:options>标签相似,他通过name属性或property属性指定一个集合对象,该对象中的每一个元素为一个Bean,并且在Bean中分别具有与标签中label属性和value属性指定的值匹配的getXXX方法。

其中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:optionsCollection
     name="xueli"
     value="userName"
     label="userValue"/>
</html:select>


eg:

*******
<html:select property="name" value="0">
<html:options collection="list" property="name" labelProperty="age"/>
<html:option value="0">无</html:option>
</html:select>
<!-- 多个复选框 -->
<logic:iterate id="item" name="mapForm" property="lvBeans">
<html:multibox name="mapForm"  property="selectedChk">
<bean:write name="item" property="value"/>
</html:multibox>
<bean:write name="item" property="label"/>
</logic:iterate><br>
<!-- 单选下拉列表 -->
<html:select  property="selBirth" value="0">
<html:option value="0">无</html:option>
<html:optionsCollection name="mapForm"  property="optionBirth" />
</html:select>
<br>
<!-- 多选下拉列表 -->
<html:select  property="selBirths" value="0" multiple="true">
<html:option value="0">无</html:option>
<html:optionsCollection name="mapForm"  property="optionBirth"/>
</html:select>

  相关解决方案