这里创建一个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 来从下拉列表中选择的值到自动填充生成的文本框中。
一个完整的struts2示例,通过利用<s:combobox>说明组合框。
action类来生成并按住选定的组合框的选项。
comboboxaction.java
package com.h3.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;
}
}
通过“<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>
<?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.h3.common.action.comboboxaction" method="display">
<result name="none">pages/combobox.jsp</result>
</action>
<action name="resultaction" class="com.h3.common.action.comboboxaction">
<result name="success">pages/result.jsp</result>
</action>
</package>
</struts>
http://localhost:8080/strut2combobox/comboboxaction.action
http://localhost:8080/strut2combobox/resultaction.action
下载代码:http://pan.baidu.com/s/1qw8ds5y

