Www.helper3.com
热门关键字: php net javascript java
Struts2基础 专题
专题目录
您的位置:java > Struts2基础专题 > Struts2 <s:optiontransferselect>示例
Struts2 <s:optiontransferselect>示例
作者:--    发布时间:2019-11-20 10:16:10

在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"  class="lazy" data-original="/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.h3.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.h3.common.action.optiontransferselectaction" 
        method="display">
	<result name="none">/pages/optiontransferselect.jsp</result>
  </action>
		
  <action name="resultaction" 
        class="com.h3.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
滚动到顶部 滚动到底部
网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
QQ群:570078429
网站备案号:
粤ICP备17073857号