html:select标签示例

需要注意的是:与select标记不同的是默认值不是在option中用selected来进行标志,而是在<html:select value="">通过value来进行标志。multiple用来设置是否允许多选,size来设置显示个数。贴一下代码吧。

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="org.apache.struts.util.LabelValueBean"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="com.mj.entity.Colors"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html:html>
<head>
<title><bean:message key="TestSelect1.title"/></title>
</head>
<body>
<html:form action="/select1.action" method="post">
<table>
<tr>
<td>
<bean:message key="TestSelect1.username"/>
</td>
<td>
<html:text property="username"/>
</td>
</tr>
<tr>
<td>
<bean:message key="TestSelect1.likes"/>
</td>
<td>
<!-- value用来设置默认值,multiple用来设置可以多选 -->
<html:select property="likes1" value="2" multiple="multiple">
<html:option value="0"><bean:message key="TestSelect1.eat"/></html:option>
<html:option value="1"><bean:message key="TestSelect1.drink"/></html:option>
<html:option value="2"><bean:message key="TestSelect1.play"/></html:option>
</html:select>
</td>
</tr>
<tr>
<td>
颜色喜好1:
</td>
<%
List list=new ArrayList();
list.add(new LabelValueBean("红色","0"));
list.add(new LabelValueBean("黄色","1"));
list.add(new LabelValueBean("绿色","2"));
session.setAttribute("list",list);
%>
<td>
<!-- 测试以下Struts提供的LabelValueBean -->
<html:select property="likes2" multiple="multiple" size="2" value="2">
<html:options collection="list" labelProperty="label" property="value"/>
</html:select>
</td>
</tr>
<tr>
<td>
颜色喜好2:
</td>
<%
List list1=new ArrayList();
list1.add(new Colors("大黄色","0"));
list1.add(new Colors("中黄色","1"));
list1.add(new Colors("小黄色","2"));
session.setAttribute("list1",list1);
%>
<td>
<!-- 测试以下Struts提供的LabelValueBean -->
<html:select property="likes3" multiple="multiple" size="2" value="2">
<html:options collection="list1" labelProperty="labelValue" property="value"/>
</html:select>
</td>
</tr>
<tr>
<td>
颜色喜好3:
</td>
<%
List list3=new ArrayList();
list3.add(new LabelValueBean("3红色","0"));
list3.add(new LabelValueBean("3黄色","1"));
list3.add(new LabelValueBean("3绿色","2"));
session.setAttribute("list3",list3);
%>
<td>
<!-- 测试以下Struts提供的LabelValueBean -->
<html:select property="likes3" multiple="multiple" size="2" value="2">
<html:optionsCollection name="list3" label="label" value="value"/>
</html:select>
</td>
</tr>
<tr>
<td>
颜色喜好4:
</td>
<%
List list4=new ArrayList();
list4.add(new Colors("4大黄色","0"));
list4.add(new Colors("4中黄色","1"));
list4.add(new Colors("4小黄色","2"));
session.setAttribute("list4",list4);
%>
<td>
<!-- 测试以下Struts提供的LabelValueBean -->
<html:select property="likes4" multiple="multiple" size="2" value="2">
<html:optionsCollection name="list4" label="labelValue" value="value"/>
</html:select>
</td>
</tr>
<tr>
<td>
<html:submit><bean:message key="TestSelect1.submit"/></html:submit>
</td>
<td>
<html:reset><bean:message key="TestSelect1.reset"/></html:reset>
</td>
</tr>
</table>
</html:form>
</body>
</html:html>

另外需要注意的是:LabelValueBean是struts提供的一个存放元素的bean,我们完全可以实现自己的javabean来进行显示。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值