Java实现后台接收传递数组参数
前台与后台数据交互一般都是将参数数据转换为json格式--进行传递,而且java的后台只能接收字符串类型的请求参数,接收后才进行类型的转换。
【1】将数组参数转换为json格式传递
前台:
function delAll(){
var checkStatus = table.checkStatus("tabVideoInfor").data;
var arruserSpaceID=[];
var arrfileMD5=[];
var arrfileUrl=[];
for ( var i = 0; i < checkStatus.length; i++) {
arruserSpaceID.push(checkStatus[i].userSpaceID);
arrfileMD5.push(checkStatus[i].fileMD5);
arrfileUrl.push(checkStatus[i].fileUrl);
}
//将数组转换为字符串
arruserSpaceID=JSON.stringify(arruserSpaceID);
arrfileMD5=JSON.stringify(arrfileMD5);
arrfileUrl=JSON.stringify(arrfileUrl);
//拼接为json格式---- {"data":["我爱你","我哼你","我打你"]}
var strjosn="{'arruserSpaceID':"+arruserSpaceID+",
'arrfileMD5':"+arrfileMD5+",'arrfileUrl':"+arrfileUrl+"}";
$.post("${ctx}/web/FileRecordServlet",{"method":"teste",
"strjosn":strjosn},function(data){
if(data=="1"){
layer.alert("删除成功!",{icon: 6} );
tabtransmitInfor.reload();
}
});
}
后台:
引用json-lib-2.4-jdk15.jar通过JSONObject类、JSONArray类的封装方法,将json字符串转为java对象
public void teste(HttpServletRequest request, HttpServletResponse response) throws IOException {
String strjosn= request.getParameter("strjosn");
System.out.println(strjosn);
//将Json字符串转为java对象
JSONObject jsonObject = JSONObject.fromObject(strjosn);
//获取ArrayObject
JSONArray IDtoArray = jsonObject.getJSONArray("arruserSpaceID");
JSONArray fileMD5toArray = jsonObject.getJSONArray("arrfileMD5");
JSONArray fileUrltoArray = jsonObject.getJSONArray("arrfileUrl");
for (int i = 0; i < IDtoArray.size(); i++) {
System.out.println("arruserSpaceID:" + IDtoArray.getString(i));
System.out.println("fileMD5toArray:" + fileMD5toArray.getString(i));
System.out.println("fileUrltoArray:" + fileUrltoArray.getString(i));
}
}
【2】其他
在我看来只要能获取到数据,一般就可以为所欲为。
还有一种方法不用什么架包,简单粗暴,我感觉是万能,自从我开始用这个方法,我不得不佩服自己。
后台接收参数,它只认字符串怎么办?那就给它字符串。。。。。。。
将参数拼接成字符串传递给后台接收,后台再进行分割
前台:
function delAll(){
var strID ="";
var strfileMD5 ="";
var strfileUrl ="";
var checkStatus = table.checkStatus("tabMusicInfor");
//将参数拼接成字符串
for ( var i = 0; i < checkStatus.data.length; i++) {
strID+=checkStatus.data[i].userSpaceID+"*";
strfileMD5+=checkStatus.data[i].fileMD5+"*";
strfileUrl+=checkStatus.data[i].fileUrl+"*";
}
strID = strID.substr(0, strID.length - 1);
strfileMD5 = strfileMD5.substr(0, strfileMD5.length - 1);
strfileUrl = strfileUrl.substr(0, strfileUrl.length - 1);
$.post("${ctx}/web/FileRecordServlet",{"method":"teste",
"strID":strID ,"strfileMD5":strfileMD5 ,"strfileUrl":strfileUrl }
,function(data){
if(data=="1"){
layer.alert("删除成功!",{icon: 6} );
tabtransmitInfor.reload();
}
});
}
后台:
在后台分割字符串时要注意
直接使用String[] strArr=strID.split("*");是不正确的,报错Dangling meta character '*' near index 0,因为+、*、|、\等符号在正则表达示中有相应的不同意义,所以在使用时要进行转义处理。
解决方法:String[] strArr=strID.split("\\*");
public void delMusic(HttpServletRequest request, HttpServletResponse response) throws IOException {
//获取请求参数
String strID= request.getParameter("strID");
String strfileMD5= request.getParameter("strfileMD5");
String strfileUrl= request.getParameter("strfileUrl");
//分割字符串
String[] arrID=strID.split("\\*");
String[] arrfileMD5=strfileMD5.split("\\*");
String[] arrfileUrl=strfileUrl.split("\\*");
for (int i = 0; i < arrID.length; i++) {
int userSpaceID=Integer.parseInt(arrID[i]);
String fileMD5=arrfileMD5[i];
String fileUrl=arrfileUrl[i];
}
}