`
u011936142
  • 浏览: 42964 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

解决Struts2中的s:optiontransferselect排序后的数据获取的问题

 
阅读更多

今天在学习struts2标签s:optiontransferselect的时候遇到一个问题,就是从新排序后的数据回传到action或后台的的问题,找了一个晚自习终于对这个很多程序猿眼中的bug解决了一点点,直接使用这个标签后运行时火狐的firebug会报错

<s:form action="orderBack" method="post">
		<s:optiontransferselect 
			leftTitle="未排序试题" name="leftList" list="list"
			listKey="id" listValue="name" headerKey="cnKey" 
			cssStyle="width:100px;height:200px"
			
			label="试题排序"
			addToLeftLabel="向左移动" addToRightLabel="向右移动"
			addAllToRightLabel="全部导入右侧" addAllToLeftLabel="全部导入左侧"
			selectAllLabel="选中所有" id="leftListId" 
			leftUpLabel="上移" leftDownLabel="下移"
			
			rightTitle="已排序试题"
			doubleList="orderList" doubleListKey="id" doubleListValue="name"
			doubleName="rightList" doubleHeaderKey="cnKey" doubleMultiple="true"
			doubleId="rightListId" 
			rightUpLabel="上移" rightDownLabel="下移"
			doubleCssStyle="width:100px;height:200px" >


		</s:optiontransferselect>
		<s:submit value="排序完成" />
	</s:form>





通过进入struts2内部的处理机制分析,是在加载util.js的时候出现的问题


解决办法就是将其解压出来从外部链接到页面,但是这里只用到了submit的提交的事件注册,所以

只用将以下代码引入就可以解决这个bug

<script type="text/javascript">
	var StrutsUtils = {};
	StrutsUtils.addEventListener = function(element, name, observer, capture) {
		if (element.addEventListener) {
			element.addEventListener(name, observer, false);
		} else if (element.attachEvent) {
			element.attachEvent('on' + name, observer);
		}
	};
</script>

这么说可能有点抽象,下面是我写的一个小事例希望能够帮助大家


index,jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>所有试题</title>
</head>
<body>
	<table border="1" width="300px">
		<tr>
			<td>顺序</td>
			<td>试题名称</td>
		</tr>
		<s:iterator value="list">
			<tr>
				<td>题号:<s:property value="id" /></td>
				<td>试题:<s:property value="name" /></td>
			</tr>
		</s:iterator>
	</table>

	<s:a href="order">试题排序</s:a>
	<s:debug />
</body>
</html>

struts2.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
	<package name="exam" namespace="/" extends="struts-default">
		<action name="findAll" class="action.QuestionAction" method="findAll">
			<result name="success">/index.jsp</result>
		</action>
		
		<action name="order" class="action.QuestionAction" method="order">
			<result name="success">/order.jsp</result>
		</action>
		
		<action name="orderBack" class="action.QuestionAction" method="orderBack">
			<result name="success">/list.jsp</result>
		</action>
	</package>


	<!-- Add packages here -->

</struts>

model类

package model;

public class Question {

	private int id;
	private int order;
	private String name;

	public Question(int id, int order, String name) {
		this.id = id;
		this.order = order;
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getOrder() {
		return order;
	}

	public void setOrder(int order) {
		this.order = order;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

action类

package action;

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

import model.Question;

import com.opensymphony.xwork2.ActionSupport;

public class QuestionAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// 未排序的数组
	private List<Question> list = new ArrayList<Question>();

	// 已排序试题
	private List<Question> orderList = new ArrayList<Question>();

	// 排序后的数组,由于之前的页面提交后会在parameters域中存入
	// parameters
	// {__multiselect_rightList=[Ljava.lang.String;@1206cda0,
	// __multiselect_leftList=[Ljava.lang.String;@7ca5cc9e,
	// rightList=[Ljava.lang.String;@691cd900}
	// 其中的rightList就是s:optiontransferselect中设置的doubleName的值
	// 这个数据会跟随提交自动的传入到action类中的同名函数进行赋值
	
	
	
	//会得到一个右边的列表排序后的id的数组
	//普通的数据类型会自动转换类型将String转换为Integer
	private Integer[] rightList;

	private List<Question> getAll() {

		List<Question> list = new ArrayList<Question>();
		Question q1 = new Question(1, 0, "试题1");
		Question q2 = new Question(2, 0, "试题2");
		Question q3 = new Question(3, 0, "试题3");
		Question q4 = new Question(4, 0, "试题4");
		list.add(q1);
		list.add(q2);
		list.add(q3);
		list.add(q4);
		return list;
	}

	// 模拟数据库
	public String findAll() {
		list = getAll();
		return SUCCESS;
	}

	// 传递数据
	public String order() {
		list = getAll();
		return SUCCESS;
	}

	public List<Question> getList() {
		return list;
	}

	public void setList(List<Question> list) {
		this.list = list;
	}

	public List<Question> getOrderList() {
		return orderList;
	}

	public void setOrderList(List<Question> orderList) {
		this.orderList = orderList;
	}

	//获取前台传入的排序后的列表的ID列表
	public Integer[] getRightList() {
		return rightList;
	}

	public void setRightList(Integer[] rightList) {
		this.rightList = rightList;
	}

	public String orderBack() {
		//直接获取原始的数据
		list = getAll();
		//遍历
		for (int i = 0; i < rightList.length; i++) {
			//通过遍历
			//从原始数据中查询数据,按照排序后的id的查询
			//如果是Map的时候直接根据key进行查询
			//rightList[i]------------获取的是排序后数组中的一个id数据,
			//由于试题的原始数据是顺序的所以通过试题id-1可以得到当前的试题的所在的位置下标
			//list.get(index);--------获取指定下标的集合中的元素
			//orderList.add();-------向排序后的集合加入数据
			//从而得到排序后的集合
			orderList.add(list.get(rightList[i] - 1));
		}
		return SUCCESS;
	}

}

order.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="sx" uri="/struts-dojo-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>排列试题</title>

<!-- 由于struts2的内部的bug必须进行手动加载util.js文件 -->
<!-- 这里只用到了StrutsUtils所以只用手动加载一下代码就可以直接用submit控件,将数据提交的后台, -->
<!-- 提交后的数据目前我只找到了doubleName指定名称的集合是重新排列 doubleListKey后的一个数组,
这个数组可以在后台通过同名函数获取-->
<script type="text/javascript">
	var StrutsUtils = {};
	StrutsUtils.addEventListener = function(element, name, observer, capture) {
		if (element.addEventListener) {
			element.addEventListener(name, observer, false);
		} else if (element.attachEvent) {
			element.attachEvent('on' + name, observer);
		}
	};
</script>




</head>

<body>

	<s:form action="orderBack" method="post">
		<s:optiontransferselect 
			leftTitle="未排序试题" name="leftList" list="list"
			listKey="id" listValue="name" headerKey="cnKey" 
			cssStyle="width:100px;height:200px"
			
			label="试题排序"
			addToLeftLabel="向左移动" addToRightLabel="向右移动"
			addAllToRightLabel="全部导入右侧" addAllToLeftLabel="全部导入左侧"
			selectAllLabel="选中所有" id="leftListId" 
			leftUpLabel="上移" leftDownLabel="下移"
			
			rightTitle="已排序试题"
			doubleList="orderList" doubleListKey="id" doubleListValue="name"
			doubleName="rightList" doubleHeaderKey="cnKey" doubleMultiple="true"
			doubleId="rightListId" 
			rightUpLabel="上移" rightDownLabel="下移"
			doubleCssStyle="width:100px;height:200px" >


		</s:optiontransferselect>
		<s:submit value="排序完成" />
	</s:form>
	<s:debug />
</body>
</html>

list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>所有试题</title>
</head>
<body>
	<table border="1" width="300px">
		<tr>
			<td>顺序</td>
			<td>试题名称</td>
		</tr>
		<s:iterator value="orderList">
			<tr>
				<td>题号:<s:property value="id" /></td>
				<td>试题:<s:property value="name" /></td>
			</tr>
		</s:iterator>
	</table>

	<s:a href="order">试题排序</s:a>
	<s:property value="#com.opensymphony.xwork2.ActionContext.parameters['leftList']"/>
	<s:debug />
</body>
</html>

web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Struts Blank</display-name>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>





分享到:
评论

相关推荐

    struts2 标签 s:optiontransferselect左右下拉列表的使用。

    NULL 博文链接:https://zhouhaitao.iteye.com/blog/1179393

    struts2 optiontransferselect标签的用法

    struts2 optiontransferselect标签,其中包含一个jsp页面,可以将此jsp页面放入您的项目中,即可看到效果!

    struts2 标签库 帮助文档

    2. &lt;s:optiontransferselect doubleList="" list="" doubleName=""&gt;&lt;/s:optiontransferselect&gt;-----左右选择框 P: 1. &lt;s:param&gt;&lt;/s:param&gt;-----为其他标签提供参数 2. &lt;s:password&gt;&lt;/s:password&gt;-----密码...

    struts2 optiontransferselect标签使用中的问题

    NULL 博文链接:https://hanxin0311.iteye.com/blog/443327

    struts2标签大全详解

    struts2标签大全详解,optiontransferselect标签属性 ,Struts2常用的Ajax标签

    Struts2+API+标签全解+网页说明

    optiontransferselect标签:创建两个选项以及转移下拉列表项,该标签生成两个下拉列表框,同时生成相应的按钮,这些按钮可以控制选项在两个下拉列表之间移动、排序。 radio标签:单选框。 optgroup标签:生成一个...

    Struts2 UI标签代码示例

    Struts2 UI标签示例(包含tabbedPanel标签,autocompleter标签,tree和treenode,optiontransferselect标签等)

    struts2相关资料

    这里包含了struts2标签的介绍,介绍了Struts2中OGNLvalueStack和stackContext,radio List的预选问题,文件的上传与下载,防止表单重复提交optiontransferselect字符串拼接,struts2_Path_路径问题

    深入浅出Struts2(附源码)

    本书是广受赞誉的Struts 2优秀教程,它全面而深入地阐述了Struts 2的各个特性,并指导开发人员如何根据遇到的问题对症下药,选择使用最合适的特性。作者处处从实战出发,在丰富的示例中直观地探讨了许多实用的技术,...

    用struts2 实现注册

    用struts2实现用户的注册,涉及到的知识点包括用户名和密码的校验(正则表达式验证);国际化;radio,checkboxlist,optiontransferselect等标签的使用,文件的上传等。

    Struts2标签 UI标志又可以分为表单UI和非表单UI两部分

    不过,Struts 2.0加了几个我们经常在项目中用到的控件如:datepicker、doubleselect、timepicker、optiontransferselect等。因为这些标志很多都经常用到,而且参数也很多,要在一篇文章详细说明并非易事。 下面主要...

    深入浅出Struts 2 .pdf(原书扫描版) part 1

    书中介绍了如何利用Struts 2 来解决Web 应用开发中的常见问题,同时还深入浅出地探讨了许多能帮助程序员编写Struts 2 应用程序的技巧,如管理页面导航活动、输入验证、国际化和本地化、对Ajax 的支持,等等。...

    JSP_struts2标签大全

    JSP_struts2标签大全 1.a 3 2.action 3 3. actionerror 4 4. actionmessage 5 5. append 5 6. bean 7 7.checkbox 7 8.checkboxlist 8 9. combobox 9 10. conponent 9 11. date 11 12. datetimepicker 12 13. debug ...

    低清版 大型门户网站是这样炼成的.pdf

    2.2.1 web.xml中struts 2的配置实现 54 2.2.2 struts 2属性配置文件struts.properties详解 55 2.2.3 struts 2核心配置文件struts.xml详解 57 2.3 struts 2应用开发实务 61 2.3.1 struts 2应用开发环境的搭建 62 ...

    JQuery实现select互换数据和上下移动

    功能类似struts2 optiontransferselect 标签的jquery组件 非常好用的一个JQuery组件

Global site tag (gtag.js) - Google Analytics