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

在struts2,<s:updownselect>标签用于创建一个带有按钮的html选择组件向上或向下移动在选择组件的选项。当提交表单时,所有的选择方案将提交在它们的排列顺序。

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

<s:updownselect> 示例

<s:updownselect
list="#{'kfc':'kfc', 'mcdonald':'mcdonald', 'burger king':'burger king',
'pizza hut':'pizza hut', 'fat boy king':'fat boy king'}"
name="favfastfood"
headerkey="-1"
headervalue="--- please order ---" 
size="7"
/>

产生下面的html代码,选择组件,按钮和javascript在选择组件向下移动的选项。 (默认的xhtml主题)

<tr> 
<td class="tdlabel"></td> 
<td><script type="text/javascript"  class="lazy" data-original="/struts2example/struts/optiontransferselect.js">
</script> 
<table> 
<tr><td> 
<select name="favfastfood" size="7" 
id="resultaction_favfastfood" multiple="multiple"> 
    <option value="-1">--- please order ---</option> 
    <option value="kfc">kfc</option> 
    <option value="mcdonald">mcdonald</option> 
    <option value="burger king">burger king</option> 
    <option value="pizza hut">pizza hut</option> 
    <option value="fat boy king">fat boy king</option> 
</select> 
<input type="hidden" id="__multiselect_resultaction_favfastfood" 
name="__multiselect_favfastfood" value="" /> 
</td></tr> 
<tr><td> 
 <input type="button" value="^" onclick="
moveoptionup(document.getelementbyid('resultaction_favfastfood'), 'key', '-1');" />

 <input type="button" value="v" onclick="
moveoptiondown(document.getelementbyid('resultaction_favfastfood'), 'key', '-1');" />

 <input type="button" value="*" onclick="selectalloptionsexceptsome(
document.getelementbyid('resultaction_favfastfood'), 'key', '-1');" />
</td></tr> 
</table></td> 
</tr> 
<script type="text/javascript"> 
 
var containingform = document.getelementbyid("resultaction");
strutsutils.addeventlistener(containingform, "submit", 
   function(evt) {
     var updownselectobj = document.getelementbyid("resultaction_favfastfood");
     selectalloptionsexceptsome(updownselectobj, "key", "-1");
   }, true);
</script>

struts2 <s:updownselect> 示例

在一个完整的全面<s:updownselect>标签例子来说明如何使用ognl和java列出的数据填充到上下按钮选择列表。

1. 动作类 - action

action类来生成和存储选择选项。

updownselectaction.java

package com.h3.common.action;

import java.util.arraylist;
import java.util.list;

import com.opensymphony.xwork2.actionsupport;

public class updownselectaction extends actionsupport{

	private list<string> movieslist = new arraylist<string>();

	private string favmovie;
	private string favfastfood;

	public updownselectaction(){
		
		movieslist.add("spider man");
		movieslist.add("matrix");
		movieslist.add("super man");
		movieslist.add("dark knight");
		movieslist.add("braveheart");
		movieslist.add("ultraman");
		
	}
	
	public string getfavfastfood() {
		return favfastfood;
	}

	public void setfavfastfood(string favfastfood) {
		this.favfastfood = favfastfood;
	}

	public list<string> getmovieslist() {
		return movieslist;
	}

	public void setmovieslist(list<string> movieslist) {
		this.movieslist = movieslist;
	}

	public string getfavmovie() {
		return favmovie;
	}

	public void setfavmovie(string favmovie) {
		this.favmovie = favmovie;
	}

	public string execute() throws exception{
		
		return success;
	}

	public string display() {
		return none;
	}
	
}

2. 结果页面

通过“<s:updownselect>”渲染上下按钮选择组件标签。

updownselect.jsp

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

<body>
<h1>struts 2 updownselect example</h1>

<s:form action="resultaction" namespace="/" method="post" >

<s:updownselect
list="#{'kfc':'kfc', 'mcdonald':'mcdonald', 'burger king':'burger king',
'pizza hut':'pizza hut', 'fat boy king':'fat boy king'}"
name="favfastfood"
headerkey="-1"
headervalue="--- please order ---" 
size="7"
/>

<s:updownselect
list="movieslist"
name="favmovie"
headerkey="-1"
headervalue="--- please order ---"
size="10"
moveuplabel="move up"
movedownlabel="move down"
selectalllabel="select all" />

<s:submit value="submit" name="submit" />
	
</s:form>

</body>
</html>

result.jsp

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

<body>
<h1>struts 2 updownselect example</h1>

<h2>
   favor fast food : <s:property value="favfastfood"/> 
</h2> 

<h2>
   favor movies : <s:property value="favmovie"/> 
</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="updownselectaction" 
       class="com.h3.common.action.updownselectaction" method="display">
       <result name="none">pages/updownselect.jsp</result>
   </action>
		
   <action name="resultaction" class="com.h3.common.action.updownselectaction" >
       <result name="success">pages/result.jsp</result>
   </action>
  </package>
	
</struts>

4. 示例

http://localhost:8080/struts2example/updownselectaction.action
选择选项,将其向上或向下。

当提交后,所有的选项都将在它们的排列顺序提交

参考

  1. struts 2 updownselect example
  2. http://www.h3.com/struts_2/struts-2-sdoubleselect-example.html

代码下载:http://pan.baidu.com/s/1hqtuady
所有评论

loading......

滚动到顶部 滚动到底部
合作联系(微信):
www.helper3.com
关于本站:
宗旨
网站备案号:
粤ICP备17073857号-1

声明:
本站部分内容来自网络,如您发现本站内容侵害到您的利益,请联系本站管理员处理。