Json格式参数传递规范
在Controller中,如果需要传递的参数超过两个,那么就要用vo进行传递,而vo中包含List类型参数时,则必须使用Json格式进行传递,以下为简单示例:
1.ActiveVo.java
package com.enation.app.shop.demo;
import java.util.List;
public class ActiveVo {
private String active_name;
private String active_time;
List<Goods> goodsList;
public String getActive_name() {
return active_name;
}
public void setActive_name(String active_name) {
this.active_name = active_name;
}
public String getActive_time() {
return active_time;
}
public void setActive_time(String active_time) {
this.active_time = active_time;
}
public List<Goods> getGoodsList() {
return goodsList;
}
public void setGoodsList(List<Goods> goodsList) {
this.goodsList = goodsList;
}
@Override
public String toString() {
for (Goods goods : goodsList) {
System.out.println(goods.getGoodsid());
}
return "ActiveVo [active_name=" + active_name + ", active_time=" + active_time + ", goodsList=" + goodsList
+ "]";
}
}
2.Goods.java
package com.enation.app.shop.demo;
public class Goods {
private int goodsid;
private int productid;
public int getGoodsid() {
return goodsid;
}
public void setGoodsid(int goodsid) {
this.goodsid = goodsid;
}
public int getProductid() {
return productid;
}
public void setProductid(int productid) {
this.productid = productid;
}
}
3.DemoController.java
package com.enation.app.shop.demo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@PostMapping("/mytest")
public ActiveVo add(@RequestBody ActiveVo vo) {
System.out.println(vo.toString());
return vo;
}
}
4.json-param-demo.html
<#include 'common/header.html' />
<body>
<form>
<input type="text" name="active_name" />
<br>
<input type="text" name="active_time" />
<br>
<input type="checkbox" name="goodsid" class="goods" productid="11" value="1" />1
<input type="checkbox" name="goodsid" class="goods" productid="22" value="2" />2
<input type="button" id="submitBtn" value="ok">
</form>
<script type="text/javascript">
(function($) {
$.fn.serializeJson = function() {
var serializeObj = {};
$(this.serializeArray()).each(function() {
serializeObj[this.name] = this.value;
});
return serializeObj;
};
})(jQuery);
$(function() {
$("#submitBtn").click(function() {
var active = $("form").serializeJson()
var goodsList = [];
$(".goods:checked").each(function(i, v) {
var goods = {};
var $this = $(v);
goods.goodsid = $this.val();
goods.productid = $(this).attr("productid");
goodsList.push(goods);
});
active["goodsList"] = goodsList;
console.log(active);
$.ajax({
url : "mytest.do",
data : JSON.stringify(active),
method : "post",
contentType : "application/json",
success : function() {
alert("ok")
},
error : function() {
alert("error")
}
});
});
});
</script>
</body>
<#include 'common/footer.html'/>
其中
(function($) {
$.fn.serializeJson = function() {
var serializeObj = {};
$(this.serializeArray()).each(function() {
serializeObj[this.name] = this.value;
});
return serializeObj;
};
})(jQuery);
可以通过引入common.js来省略
http://www.javamall.com.cn/help/index.php/全局公用脚本使用说明#.E8.A1.A8.E5.8D.95.E5.BA.8F.E5.88.97.E5.8C.96.E4.B8.BAJson
运行后的效果: