javaweb-SpringMVC下怎么使用List数据绑定?ajax传输应该怎么写
2个回答
展开全部
//将一个表单的数据返回成JSON对象
$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [ o[this.name] ];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
$(document).ready(
function() {
jQuery.ajax( {
type : 'GET',
contentType : 'application/json',
url : 'user/list',
dataType : 'json',
success : function(data) {
if (data && data.success == "true") {
$('#info').html("共" + data.total + "条数据。
");
$.each(data.data, function(i, item) {
$('#info').append(
"编号:" + item.id + ",姓名:" + item.username
+ ",年龄:" + item.age);
});
}
},
error : function() {
alert("error")
}
});
$("#submit").click(function() {
var jsonuserinfo = $.toJSON($('#form').serializeObject());
alert(jsonuserinfo);
jQuery.ajax( {
type : 'POST',
contentType : 'application/json',
url : 'user/add',
data : jsonuserinfo,
dataType : 'json',
success : function(data) {
alert("新增成功!");
},
error : function(data) {
alert("error")
}
});
});
});
@Controller
@RequestMapping("/user")
public class DemoController {
private Logger logger = LoggerFactory.getLogger(DemoController.class);
@RequestMapping(value = "/list", method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> getUserList() {
logger.info("列表");
List<UserModel> list = new ArrayList<UserModel>();
UserModel um = new UserModel();
um.setId("1");
um.setUsername("sss");
um.setAge(222);
list.add(um);
Map<String, Object> modelMap = new HashMap<String, Object>(3);
modelMap.put("total", "1");
modelMap.put("data", list);
modelMap.put("success", "true");
return modelMap;
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
@ResponseBody
public Map<String, String> addUser(@RequestBody UserModel model) {
logger.info("新增");
logger.info("捕获到前台传递过来的Model,名称为:" + model.getUsername());
Map<String, String> map = new HashMap<String, String>(1);
map.put("success", "true");
return map;
}
}
$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [ o[this.name] ];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
$(document).ready(
function() {
jQuery.ajax( {
type : 'GET',
contentType : 'application/json',
url : 'user/list',
dataType : 'json',
success : function(data) {
if (data && data.success == "true") {
$('#info').html("共" + data.total + "条数据。
");
$.each(data.data, function(i, item) {
$('#info').append(
"编号:" + item.id + ",姓名:" + item.username
+ ",年龄:" + item.age);
});
}
},
error : function() {
alert("error")
}
});
$("#submit").click(function() {
var jsonuserinfo = $.toJSON($('#form').serializeObject());
alert(jsonuserinfo);
jQuery.ajax( {
type : 'POST',
contentType : 'application/json',
url : 'user/add',
data : jsonuserinfo,
dataType : 'json',
success : function(data) {
alert("新增成功!");
},
error : function(data) {
alert("error")
}
});
});
});
@Controller
@RequestMapping("/user")
public class DemoController {
private Logger logger = LoggerFactory.getLogger(DemoController.class);
@RequestMapping(value = "/list", method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> getUserList() {
logger.info("列表");
List<UserModel> list = new ArrayList<UserModel>();
UserModel um = new UserModel();
um.setId("1");
um.setUsername("sss");
um.setAge(222);
list.add(um);
Map<String, Object> modelMap = new HashMap<String, Object>(3);
modelMap.put("total", "1");
modelMap.put("data", list);
modelMap.put("success", "true");
return modelMap;
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
@ResponseBody
public Map<String, String> addUser(@RequestBody UserModel model) {
logger.info("新增");
logger.info("捕获到前台传递过来的Model,名称为:" + model.getUsername());
Map<String, String> map = new HashMap<String, String>(1);
map.put("success", "true");
return map;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2017-06-05 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
public class Certificates {
private List<Certificate> certsList = new ArrayList<Certificate>();
public List<Certificate> getCertsList() {
return certsList;
}
public void setCertsList(List<Certificate> certsList){
this.certsList = certsList;
}
}
$.ajax({
async : false,
cache : false,
type : "post",
dataType : "json",
dataType : "text",
contentType : "application/json;charset=UTF-8",
data : JSON.stringify($('#infoForm').serializeObject()),
url : "insertExpert.htm",
success : function(data) {
var d = $.parseJSON(data);
if (d.success) {
alert("注册信息成功!\n请用注册的用户登陆系统,上传代码证附件!");
} else {
alert("注册失败!");
}
}
});
<tr id="table_zgzs_row">
<td><input type="text" name="certsList[0].zgzsmc" id="certs[0].zgzsmc"
style="width:100%;height:20px" />
</td>
<td><input type="text" name="certsList[0].zsbh00" id="certs[0].zsbh00"
style="width:100%;height:20px" />
</td>
<td><input type="text" name="certsList[0].bfsj00" id="certs[0].bfsj00"
style="width:100%;height:20px" />
</td>
<td><input type="text" name="certsList[0].bzjg00" id="certs[0].bzjg00"
style="width:100%;height:20px" />
</td>
</tr>
private List<Certificate> certsList = new ArrayList<Certificate>();
public List<Certificate> getCertsList() {
return certsList;
}
public void setCertsList(List<Certificate> certsList){
this.certsList = certsList;
}
}
$.ajax({
async : false,
cache : false,
type : "post",
dataType : "json",
dataType : "text",
contentType : "application/json;charset=UTF-8",
data : JSON.stringify($('#infoForm').serializeObject()),
url : "insertExpert.htm",
success : function(data) {
var d = $.parseJSON(data);
if (d.success) {
alert("注册信息成功!\n请用注册的用户登陆系统,上传代码证附件!");
} else {
alert("注册失败!");
}
}
});
<tr id="table_zgzs_row">
<td><input type="text" name="certsList[0].zgzsmc" id="certs[0].zgzsmc"
style="width:100%;height:20px" />
</td>
<td><input type="text" name="certsList[0].zsbh00" id="certs[0].zsbh00"
style="width:100%;height:20px" />
</td>
<td><input type="text" name="certsList[0].bfsj00" id="certs[0].bfsj00"
style="width:100%;height:20px" />
</td>
<td><input type="text" name="certsList[0].bzjg00" id="certs[0].bzjg00"
style="width:100%;height:20px" />
</td>
</tr>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询