项目场景:
layui-mini + spring boot
使用table.reload实现搜索操作的传参
问题描述:
layui向后端传的json格式数据
因table.reload在传参时默认携带page和limit参数,导致@requestParam、@requestBody和Servlet 无法接收到实体JSON数据。
解决方案:
使用最原始的方法,将json格式数据全部读入,在controller里做手动解析
// 监听搜索操作
form.on('submit(data-search-btn)', function (data) {
var positions = [];
$('input[type=checkbox]:checked').each(function() {
positions.push($(this).val());
});
data = data.field;
console.log(JSON.stringify(data))
var result = {
"positions":positions,
"code":data.code,
"name":data.name,
"startTime":data.startTime,
"endTime":data.endTime,
"minSalary":data.minSalary,
"maxSalary":data.maxSalary
};
result = JSON.stringify(result)
console.log(result)
//执行搜索重载
table.reload('currentTableId', {
url:"/stuff/search.do",
contentType : "application/json",
dataType:"JSON",
traditional:true,//防止深度序列化
method:"POST",
where: {
searchStuff: result
}
}, 'data');
return false;
});
Controller
@RequestMapping("/search.do")
@ResponseBody
public Resp searchDo(@RequestBody JSONObject jsonParam){
SearchStuff searchStuff = JSON.toJavaObject(JSON.parseObject(jsonParam.get("searchStuff").toString()), SearchStuff.class);
if(searchStuff.getPositions().length==0)
searchStuff.setPositions(null);
System.out.println(searchStuff);
List<Stuff> list = stuffServer.searchByCodeNamePositionTimeAndSalary(searchStuff);
Resp resp = new Resp(RespCode.SUCCESS, list, list.size());
return resp;
}