📄 html-options.jsp
字号:
<%@ page contentType="text/html;charset=GB2312" import="java.util.*"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html>
<head>
<html:base/>
<title><html:options></title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body bgcolor="white">
<h1><html:options></h1>
<p><html:options>标记用来表示一组选择项,也就是相当于一组的<html:option>标记。在一个
<html:select>标记中可以包含多个<html:options>标记。以下是一段代码示例:</p>
<font class="CodeStyle">
<%<br>
ArrayList list = new ArrayList();<br>
list.add(new org.apache.struts.util.LabelValueBean("Show value1","value1"));<br>
list.add(new org.apache.struts.util.LabelValueBean("Show value2","value2"));<br>
list.add(new org.apache.struts.util.LabelValueBean("Show value3","value3"));<br>
list.add(new org.apache.struts.util.LabelValueBean("Show value4","value4"));<br>
pageContext.setAttribute("valuelist",list);<br>
%><br>
<html:form action="/test.do"><br>
<html:select property="testString"><br>
<html:options collection="valueslist" property="value" labelProperty="label"/><br>
</html:select><br>
</html:form><br>
</font>
<p>这是一段<html:options>标记的应用方式。首先,页面创建一个ArrayList型的对象list,以此作为存放所有选项的
容器。接下来,将四个org.apache.struts.util.LabelValueBean类型的对象放入list中,在初始化这四个LabelValueBean对象时,
对每个对象的构造函数传入两个字符型参数。第一个参数将赋给LabelValueBean对象中的label变量,这个变量的值将成为页面选
项中用户所见到的部分;第二个参数将赋给LabelValueBean对象中的value变量,该变量的值就是用户在选择页面中这个选项
后传递给后台ActionForm的值,也就是<html:option>标记中value属性的值。</p>
<p>在四个对象都存入list对象后,程序把list对象放入pageContext中。这样做的目的是为了让<html:options>标记来引用
它。<html:options>标记有一个collection属性,它可以在页面上下文pageContext中指定一个实现了List接口的对象,然后
将该对象列表中所有存储的对象遍历,取出每一个对象相应的属性值,用来作为页面选项(也就是每一个option)的value和label。
如在本例中,collection属性首先指定了从pageContext中取出valuelist对象,由于valuelist对象中存储的是一个
org.apache.struts.util.LabelValueBean类型的对象列表,所以,<:html:options>标记会将每个对象取出,用
org.apache.struts.util.LabelValueBean对象中的value属性值赋给标记的property属性,再用对象中的label属性值赋给标记的
labelProperty属性。这样就将存储在valuelist中的对象和页面上的每一个选项建立起了一一对应的关系,<:html:options>
的功能也就实现了。</p>
<p>以下是代码实际运行效果:</p>
<%
ArrayList list = new ArrayList();
list.add(new org.apache.struts.util.LabelValueBean("Show value1","value1"));
list.add(new org.apache.struts.util.LabelValueBean("Show value2","value2"));
list.add(new org.apache.struts.util.LabelValueBean("Show value3","value3"));
list.add(new org.apache.struts.util.LabelValueBean("Show value4","value4"));
pageContext.setAttribute("valuelist",list);
%>
<html:form action="/test.do">
<html:select property="testString" size="1">
<html:options collection="valuelist" property="value" labelProperty="label"/>
</html:select>
<html:submit value="submit" property="submit"/>
</html:form>
<p>
<p></p>
<a href="index.jsp">返回主页面</a>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -