这里创建一个Web工程:strut2combobox,来演示在多个复选框如何设置的默认值,整个项目的结构如下图所示:
在Struts2, <s:combobox>标签是一个下拉列表单文本框组合在一起,允许用户直接输入一个值在文本框中,或选择从下拉列表中选择值,并选定值将自动填充到文本框中。
如果下拉列表和组合框列表,请阅读维基组合框定义以免混淆。
<s:combobox label="What's your favor fruit" headerKey="-1" headerValue="--- Select ---" list="fruits" name="yourFruits" />
产生下面的HTML代码...
<td class="tdLabel">
<label for="resultAction_yourFruits" class="label">
What's your favor fruit:
</label>
</td>
<td>
<script type="text/javascript">
function autoPopulate_resultAction_yourFruits(targetElement) {
if (targetElement.options[targetElement.selectedIndex].value == '-1') {
return;
}
targetElement.form.elements['yourFruits'].value=
targetElement.options[targetElement.selectedIndex].value;
}
</script>
<input type="text" name="yourFruits" value="" id="resultAction_yourFruits"/>
<br />
<select onChange="autoPopulate_resultAction_yourFruits(this);">
<option value="-1">--- Select ---</option>
<option value="Apple">Apple</option>
<option value="Banana">Banana</option>
<option value="Orange">Orange</option>
<option value="Watermelon">Watermelon</option>
</select>
</td>
<s:combobox> 标记将产生输入文本框,下拉列表中有“onChange()”方法调用来生成的JavaScript 来从下拉列表中选择的值到自动填充生成的文本框中。
如要创建一个下拉列表,应该使用 <s:select>标签来代替。
Struts 2 <s:combobox> 示例
一个完整的Struts2示例,通过利用<s:combobox>说明组合框。
1. 动作 - Action
Action类来生成并按住选定的组合框的选项。
ComboBoxAction.java
package com.xuhuhu.common.action;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class ComboBoxAction extends ActionSupport{
private List<String> fruits;
private String yourFruits;
private String yourMonth;
public String getYourMonth() {
return yourMonth;
}
public void setYourMonth(String yourMonth) {
this.yourMonth = yourMonth;
}
public List<String> getFruits() {
return fruits;
}
public void setFruits(List<String> fruits) {
this.fruits = fruits;
}
public String getYourFruits() {
return yourFruits;
}
public void setYourFruits(String yourFruits) {
this.yourFruits = yourFruits;
}
public ComboBoxAction(){
fruits = new ArrayList<String>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Watermelon");
}
public String execute() {
return SUCCESS;
}
public String display() {
return NONE;
}
}
2. 结果页面
通过“<s:combobox>”标签渲染组合框,并填充通过Java列表,OGNL列表中选择选项
combobox.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
<body>
<h1>Struts 2 <s:combobox> example</h1>
<s:form action="resultAction" namespace="/">
<h2>
<s:combobox label="What's your favor fruit"
headerKey="-1" headerValue="--- Select ---"
list="fruits"
name="yourFruits" />
</h2>
<h2>
<s:combobox label="Select a month"
headerKey="-1" headerValue="--- Select ---"
list="#{'1':'Jan', '2':'Feb', '3':'Mar', '4':'Apr'}"
name="yourMonth" />
</h2>
<s:submit value="submit" name="submit" />
</s:form>
</body>
</html>
result.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <body> <h1>Struts 2 <s:combobox> example</h1> <h2> Favor fruit : <s:property value="yourFruits"/> </h2> <h2> Selected month : <s:property value="yourMonth"/> </h2> </body> </html>
3. struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="comboBoxAction"
class="com.xuhuhu.common.action.ComboBoxAction" method="display">
<result name="none">pages/combobox.jsp</result>
</action>
<action name="resultAction" class="com.xuhuhu.common.action.ComboBoxAction">
<result name="success">pages/result.jsp</result>
</action>
</package>
</struts>
5. 示例
http://localhost:8080/strut2combobox/comboBoxAction.action

http://localhost:8080/strut2combobox/resultAction.action

参考
下载代码:http://pan.baidu.com/s/1qW8Ds5Y
上一篇:
Struts2自动选择下拉框的值
下一篇:
Struts2 <s:head>示例
