js傳遞二維陣列到java後臺 0人關注 講義教程 資訊 常見問題 開發技巧
思路:前臺使用jquery.ajax吧二維陣列傳送到java後臺,Java後臺能獲取到一維的陣列,需要將一維的陣列再次進行切割,java後臺使用字串的切割類StringTokenizer將收到的一維陣列按照指定的分隔符進行切割,前臺傳送過來的陣列預設以“,”分開,所以使用逗號作為切割符號。重點是紅色標記的部分。
前臺程式碼:
<%@ page language="java" contentType="text/html; charset=gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'main.jsp' starting page</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function test(){
var time1=new rray();
for(var i=0;i<10;i++)
{time1[i]=new Array();
for(var j=0;j<10;j++)
time1[i][j]=i*j+j;
}
jQuery.ajax({
type : "POST",
url : "calculate.action?data="+new Date(),
data:{ec:10,cc:10,cp:cp,ep:ep,time1:time1}, //cc是陣列的行數,ec是陣列的列數,cp,ep可以忽略,time1是個二維陣列
dataType : "json",
success : function(data) {
var result1=data.result1;
alert(result1.length);
},
error : function(request, error, exception)
{
alert("資料獲取錯誤: " + error + " 異常:" + exception);
}
});
}
</script>
</head>
<body onload="test()">
</body>
</html>
web.xml
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
jar包:
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>
<package name="project" extends="struts-default">
<result-types>
<result-type name="rawtext" class="RawTextResult" />
</result-types>
<action name="calculate" class="Calculate">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
java類:Calculte:
import java.util.List;
import java.util.StringTokenizer;
import javax.servlet.http.HttpServletRequest;
import com.opensymphony.xwork2.ActionSupport;
public class Calculate extends ActionSupport {
private int ec;
private int cc;
private int[] cp;
private int[] ep;
private double[][] time1;
private double changliang;
private String stringResult;
HttpServletRequest request;
public HttpServletRequest getRequest() {
return request;
}
public void setRequest(HttpServletRequest request) {
this.request = request;
}
public String getStringResult() {
return stringResult;
}
public void setStringResult(String stringResult) {
this.stringResult = stringResult;
}
public double getChangliang() {
return changliang;
}
public void setChangliang(double changliang) {
this.changliang = changliang;
}
public int getEc() {
return ec;
}
public void setEc(int ec) {
this.ec = ec;
}
public int getCc() {
return cc;
}
public void setCc(int cc) {
this.cc = cc;
}
public int[] getCp() {
return cp;
}
public void setCp(int[] cp) {
this.cp=new int[this.cc];
this.cp = cp;
}
public int[] getEp() {
return ep;
}
public void setEp(int[] ep) {
this.ep=new int[this.ec];
this.ep = ep;
}
public void setTime1(String[] time2){
this.time1=new double[cc][ec];
String[] t=new String[cc];
t=time2;
for(int i=0;i<cc;i++){
System.out.println(t[i]);
StringTokenizer st=new StringTokenizer(t[i],",");
int j=st.countTokens();
if(j==ec){
int y=0;
while(st.hasMoreElements()){
String x=st.nextToken();
System.out.println(x);
this.time1[i][y]=Double.parseDouble(x);
y++;
}
}
}
}
public String execute() throws Exception {
return "success";
}
}