今天在學習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>