Struts2 组合框示例

下载它– Struts-ComboBox-Example.zip

在Struts 2中, <s:combobox>标记基本上是一个与单行文本框组合在一起下拉列表 ,允许用户在文本框中直接输入值或从下拉列表中选择值,所选的值将自动填充到文本框中。

如果您在下拉列表和组合框列表之间感到困惑,请从Wiki阅读组合框定义

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

一个完整的Struts 2示例,通过<s:combobox>展示了组合框的用法

1.行动

动作类,用于生成并保留选定的组合框选项。
ComboBoxAction.java

package com.mkyong.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 &lt;s:combobox&gt; 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 &lt;s:combobox&gt; 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.mkyong.common.action.ComboBoxAction" method="display">
	<result name="none">pages/combobox.jsp</result>
   </action>
		
   <action name="resultAction" class="com.mkyong.common.action.ComboBoxAction">
	<result name="success">pages/result.jsp</result>
   </action>

</package>
	
</struts>

5.演示

http:// localhost:8080 / Struts2Example / comboBoxAction.action

Struts 2 combo box example

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

Struts 2 combo box example

参考

  1. Struts 2组合框文档
  2. Wiki组合框定义

翻译自: https://mkyong.com/struts2/struts-2-scombobox-combo-box-example/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值