SpringMVC舊的寫法
在SpringMVC項目中寞蚌,通常是用controller層來接受前端傳過來的數(shù)據(jù)椭蹄,但是我們目前都是用JSONObject來接收值,需要多做一步解析jsonObject的動作 例子如下:
$.ajax({
url:"<%=contextPath%>/entry/saveOrUpdateEntry",
type:'POST',
data:nui.encode(json),
cache:false,
contentType:'application/json',
success:function(text){
}
});
@ResponseBody
@RequestMapping(value = "/saveOrUpdateEntry")
public Map<String, Object> saveOrUpdateEntry(@RequestBody JSONObject jsonObject , HttpSession session) {
·····
優(yōu)化寫法
一個參數(shù)對象情況
json = {"empCode":"80489179"}
$.ajax({
url:"<%=contextPath%>/entry/saveOrUpdateEntry",
type:'POST',
data:nui.encode(json),
cache:false,
contentType:'application/json',
success:function(text){
}
});
@ResponseBody
@RequestMapping(value = "/saveOrUpdateEntry")
public Map<String, Object> saveOrUpdateEntry(String empCode , HttpSession session) {
·····
一個bean的情況
json = {"empCode":"80489179","empName":"李明”净赴,“age":"15"}
$.ajax({
url:"<%=contextPath%>/entry/saveOrUpdateEntry",
type:'POST',
data:nui.encode(json),
cache:false,
contentType:'application/json',
success:function(text){
}
});
@ResponseBody
@RequestMapping(value = "/saveOrUpdateEntry")
public Map<String, Object> saveOrUpdateEntry(@RequestBody User user, HttpSession session) {
class User{
private String empCode;
private String empName;
private Integer age;
···
}
一個Bean+一個list情況
json = {"empCode":"80489179","empName":"李明","age":"15","list":[{"id":"1","name":"小啊"},{"id":"2","name":"小明"}]}
$.ajax({
url:"<%=contextPath%>/entry/saveOrUpdateEntry",
type:'POST',
data:nui.encode(json),
cache:false,
contentType:'application/json',
success:function(text){
}
});
@ResponseBody
@RequestMapping(value = "/saveOrUpdateEntry")
public Map<String, Object> saveOrUpdateEntry(@RequestBody EmpVo empvo, HttpSession session) {
class EmpVo{
private String empCode;
private String empName;
private Integer age;
private List<Dict> list;
绳矩。。玖翅。
翼馆。割以。。
应媚。严沥。
}
class Dict{
private Integer id;
private String name;
}
多個Bean+一個list情況
json = {"user":{"empCode":"80489179","empName":"李明”,"age":"15"},"list":[{"id":"1","name":"小啊"},{"id":"2","name":"小明"}],"like":{"hobby":"我的愛好","spcial":"沒有系好"}}
$.ajax({
url:"<%=contextPath%>/entry/saveOrUpdateEntry",
type:'POST',
data:nui.encode(json),
cache:false,
contentType:'application/json',
success:function(text){
}
});
@ResponseBody
@RequestMapping(value = "/saveOrUpdateEntry")
public Map<String, Object> saveOrUpdateEntry(@RequestBody Vo vo, HttpSession session) {
class Vo{
private User user;
private Like like;
private List<Dict> list;
···
}
class Dict{
private Integer id;
private String name;
}
class Like{
private String hobby;
private String spcial;
}
class User{
private String empCode;
private String empName;
private Integer age;
···
}