在上传一组图片的地址到后台时,遇到了不少的麻烦,最后勉强解决了,下面贴出关键的代码
1.js->这里创建一个json数组pathArray,然后分别将图片路径放入数组,再将数组转为json型字符串;将字符串封装到对象paraObj里;通过ajax传到后台
var pathArray=new Array();
$("#pics>div>img").each(function(){
var path=$(this).attr("src");
var id = $(this).closest("div").attr("_id");
if(path.indexOf("uploadtemp")==0){ //最后一张图片是用来作按钮的不上传
if(typeof id != 'undefined'){
pathArray.push('{"picId":'+id+',"picPath":"'+path+'"}');
}else{
pathArray.push('{"picPath":"'+path+'"}');
}
}
});
if(pathArray.length !=0){
paraObj["picJson"]='['+pathArray.join(",")+']';
}
top.showLoading();
$.ajax({
url: "task/save",
type: "post", // 数据发送方式
dataType : "json", // 接受数据格式
data : paraObj,
2.controller->这里用String来接收,然后解析json字符串,得到list; @ResponseBody
@RequestMapping(value="/save", method=RequestMethod.POST)
public Object saveTask(@ModelAttribute("task") Task task,@ModelAttribute("picJson") String picJson){
try{
List<Taskpics> picList = JSONArray.parseArray(picJson, Taskpics.class);//解析json
task.setPicList(picList);
3.这样转来转去有点麻烦,实在没别的办法时可以试试