当前位置: 首页 > 教程 > Struts2 >

Struts2 <s:optiontransferselect>示例

精华
小牛编辑
146浏览
2023-03-14

在Struts2中,选项选择组件是两个“updownselect”选择部件在左,右侧对齐,在它们中间,包含按钮来移动自己的选择选项。通过<s:optiontransferselect>标签这可以创建。

这里创建一个Web工程:strut2updownselect,来演示在多个复选框如何设置的默认值,整个项目的结构如下图所示:

<s:optiontransferselect
     label="Lucky Numbers"
     name="leftNumber"
     list="{'1 - One ', '2 - Two', '3 - Three', '4 - Four', '5 - Five'}"
     doubleName="rightNumber"
     doubleList="{'10 - Ten','20 - Twenty','30 - Thirty','40 - Forty','50 - Fifty'}"
 />
“name”和“list”是指向左选择组件;而“doubleName”和“doubleList”是指在正确的选择组。

产生下面的HTML,两个“updownselect”组件,按钮和JavaScript来自己动(默认的XHTML主题)之间的选择选项。

<tr> 
<td class="tdLabel">
<label for="resultAction_leftNumber" class="label">Lucky Numbers:</label>
</td> 
<td>
<script type="text/javascript" src="/Struts2Example/struts/optiontransferselect.js">
</script> 
<table border="0"> 
<tr> 
<td> 
<select name="leftNumber" size="15" 
id="resultAction_leftNumber" multiple="multiple"> 
    <option value="1 - One ">1 - One </option> 
    <option value="2 - Two">2 - Two</option> 
    <option value="3 - Three">3 - Three</option> 
    <option value="4 - Four">4 - Four</option> 
    <option value="5 - Five">5 - Five</option> 
</select> 
<input type="hidden" id="__multiselect_resultAction_leftNumber" 
name="__multiselect_leftNumber" value="" /> 
<input type="button"
	onclick="moveOptionDown(
        document.getElementById('resultAction_leftNumber'), 'key', '');"
	value="v"
/> 
<input type="button"
	onclick="moveOptionUp(
        document.getElementById('resultAction_leftNumber'), 'key', '');"
	value="^"
/> 
</td> 
<td valign="middle" align="center"> 
<input type="button"
    value="<-" onclick="moveSelectedOptions(
    document.getElementById('resultAction_rightNumber'), 
    document.getElementById('resultAction_leftNumber'), false, '');" />
<input type="button"
    value="->" onclick="moveSelectedOptions(
    document.getElementById('resultAction_leftNumber'), 
    document.getElementById('resultAction_rightNumber'), false, '');" /> 
<input type="button"
    value="<<--" onclick="moveAllOptions(
    document.getElementById('resultAction_rightNumber'), 
    document.getElementById('resultAction_leftNumber'), false, '');" />
<input type="button"
    value="-->>" onclick="moveAllOptions(
    document.getElementById('resultAction_leftNumber'), 
    document.getElementById('resultAction_rightNumber'), false, '');" />
<input type="button"
    value="<*>" onclick="selectAllOptions(
    document.getElementById('resultAction_leftNumber'));
    selectAllOptions(document.getElementById('resultAction_rightNumber'));" />
</td> 
<td> 
<select 
	name="rightNumber"
	size="15"
	multiple="multiple"
	id="resultAction_rightNumber"
> 
    	<option value="10 - Ten">10 - Ten</option> 
    	<option value="20 - Twenty">20 - Twenty</option> 
    	<option value="30 - Thirty">30 - Thirty</option> 
    	<option value="40 - Forty">40 - Forty</option> 
    	<option value="50 - Fifty">50 - Fifty</option> 
</select> 
<input type="hidden" id="__multiselect_resultAction_rightNumber" 
name="__multiselect_rightNumber" value="" /> 
<input type="button"
   onclick="moveOptionDown(
   document.getElementById('resultAction_rightNumber'), 'key', '');"
   value="v"
/> 
<input type="button"
   onclick="moveOptionUp(
   document.getElementById('resultAction_rightNumber'), 'key', '');"
   value="^"
/> 
</td> 
</tr> 
</table> 

<script type="text/javascript"> 
var containingForm = document.getElementById("resultAction");
StrutsUtils.addEventListener(containingForm, "submit", 
  function(evt) {
	var selectObj = document.getElementById("resultAction_leftNumber");
		selectAllOptionsExceptSome(selectObj, "key", "");
  }, true);
var containingForm = document.getElementById("resultAction");
StrutsUtils.addEventListener(containingForm, "submit", 
  function(evt) {
	var selectObj = document.getElementById("resultAction_rightNumber");
		selectAllOptionsExceptSome(selectObj, "key", "");
	}, true);
</script>

Struts2 <s:optiontransferselect> 示例

一个完整的全面的 <s:optiontransferselect> 标签例子,表明使用OGNL和Java列出来填充数据到“选项中选择转移”的组件。

1. 动作类

Action类来生成并存储左右选择选项。

OptionTransferSelectAction.java

package com.yiibai.common.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class OptionTransferSelectAction extends ActionSupport{

	private List<String> leftAntivirusList = new ArrayList<String>();
	private List<String> rightAntivirusList = new ArrayList<String>();

	private String leftAntivirus;
	private String rightAntivirus;

	private String leftNumber;
	private String rightNumber;
	
	public OptionTransferSelectAction(){
		
		leftAntivirusList.add("Norton 360 Version 4.0");
		leftAntivirusList.add("McAfee Total Protection 2010");
		leftAntivirusList.add("Trend Micro IS Pro 2010");
		leftAntivirusList.add("BitDefender Total Security 2010");

		rightAntivirusList.add("Norton Internet Security 2010");
		rightAntivirusList.add("Kaspersky Internet Security 2010");
		rightAntivirusList.add("McAfee Internet Security 2010");
		rightAntivirusList.add("AVG Internet Security 2010");
		rightAntivirusList.add("Trend Micro Internet Security 2010");
		rightAntivirusList.add("F-Secure Internet Security 2010");
		
	}
	
	public String getLeftNumber() {
		return leftNumber;
	}

	public void setLeftNumber(String leftNumber) {
		this.leftNumber = leftNumber;
	}

	public String getRightNumber() {
		return rightNumber;
	}

	public void setRightNumber(String rightNumber) {
		this.rightNumber = rightNumber;
	}

	public List<String> getLeftAntivirusList() {
		return leftAntivirusList;
	}

	public void setLeftAntivirusList(List<String> leftAntivirusList) {
		this.leftAntivirusList = leftAntivirusList;
	}

	public List<String> getRightAntivirusList() {
		return rightAntivirusList;
	}

	public void setRightAntivirusList(List<String> rightAntivirusList) {
		this.rightAntivirusList = rightAntivirusList;
	}

	public String getLeftAntivirus() {
		return leftAntivirus;
	}

	public void setLeftAntivirus(String leftAntivirus) {
		this.leftAntivirus = leftAntivirus;
	}

	public String getRightAntivirus() {
		return rightAntivirus;
	}

	public void setRightAntivirus(String rightAntivirus) {
		this.rightAntivirus = rightAntivirus;
	}

	public String execute() throws Exception{
		
		return SUCCESS;
	}

	public String display() {
		return NONE;
	}
	
}

2. 结果页面

通过“<s:optiontransferselect>”选项转移选择组件标签渲染,并通过Java和OGNL列表产生左侧和右侧选择选项。

optiontransferselect.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<s:head />
</head>

<body>
<h1>Struts 2 optiontransferselect 示例</h1>

<s:form action="resultAction" namespace="/" method="POST" >

<s:optiontransferselect
     label="Lucky Numbers"
     name="leftNumber"
     list="{'1 - One ', '2 - Two', '3 - Three', '4 - Four', '5 - Five'}"
     doubleName="rightNumber"
     doubleList="{'10 - Ten','20 - Twenty','30 - Thirty','40 - Forty','50 - Fifty'}"
 />
	
<s:optiontransferselect
     label="Favourite Antivirus"
     name="leftAntivirus"
     leftTitle="Left Antivirus Title"
     rightTitle="Right Antivirus Title"
     list="leftAntivirusList"
     multiple="true"
     headerKey="-1"
     headerValue="--- Please Select ---"
     doubleList="rightAntivirusList"
     doubleName="rightAntivirus"
     doubleHeaderKey="-1"
     doubleHeaderValue="--- Please Select ---"
 />
 
<s:submit value="submit" name="submit" />
	
</s:form>

</body>
</html>

result.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>

<body>
<h1>Struts 2 optiontransferselect example</h1>

<h2>
   Left AntiVirus : <s:property value="leftAntivirus"/> 
</h2> 

<h2>
   Right AntiVirus : <s:property value="rightAntivirus"/> 
</h2> 

<h2>
   Left Numbers : <s:property value="leftNumber"/> 
</h2> 

<h2>
   Right Numbers : <s:property value="rightNumber"/> 
</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="optionTransferSelectAction" 
	class="com.yiibai.common.action.OptionTransferSelectAction" 
        method="display">
	<result name="none">/pages/optiontransferselect.jsp</result>
  </action>
		
  <action name="resultAction" 
        class="com.yiibai.common.action.OptionTransferSelectAction" >
	<result name="success">/pages/result.jsp</result>
  </action>
</package>
	
</struts>

4. 实例

http://localhost:8080/struts2optiontransferselect/optionTransferSelectAction.action

参考

  1. Struts 2 updownselect 文档
  2. Struts 2 updownselect 示例
  3. Struts 2 doubleselect 示例

代码下载: http://pan.baidu.com/s/1qW5p8lu