文章目录提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
- 前言
- 使用springboot+layui编写后台管理系统遇到的一些问题,以及如何解决:
- 一、使用layui进行方法渲染列表时渲染不上,返回的数据不符合规范,正确的成功状态码应为:"code": 0?
- 总结:
前言 使用springboot+layui编写后台管理系统遇到的一些问题,以及如何解决: 一、使用layui进行方法渲染列表时渲染不上,返回的数据不符合规范,正确的成功状态码应为:“code”: 0?
页面中列表主要代码
//这里只有主要的代码
table.render({
elem : '#test'
,url : '/userController/findAll'
,toolbar : '#toolbarDemo'
,method: "post"
,contentType: "application/json"
,id : 'tableReload'
,page: true
,parseData : function (data) {
debugger
return {
"code" : data.code,
"msg" : data.msg,
"count": data.total,
}
}
,cols: [[
{field:'user_id', width:77, title: '序号', sort: true}
,{field:'user_name', width:200, title: '用户昵称'}
,{field:'user_pwd', width:200, title: '用户密码'}
,{field:'user_tel', width:200 , title: '用户联系方式'}
,{field:'user_email', width:200, title: '用户邮箱'}
]]
后台主要代码
@RequestMapping(value = "/findAll",method = RequestMethod.POST)
@ResponseBody
public List findAll(
@RequestBody(required = false) User user,
@RequestParam(defaultValue = "10") Integer page,
@RequestParam(defaultValue = "1") Integer size
){
List all = userService.findAll(user,page,size);
return all;
}
而且页面中也显示能获取到后台返回的数据,这就让我很纳闷,明明后台也返回数据了前台也就收到数据了而且渲染的时候字段也都是一样的就是渲染不上。。。。
其实layui官网是给了解释,
哎这就是不好好看官方文档,结果白白忙了一小时也没有改好,最后终于晓得返回的格式不对,然后就是后台创建一个公用的返回对象。
package cn.gok.common; import com.fasterxml.jackson.annotation.JsonInclude; import java.io.Serializable; @JsonInclude(value= JsonInclude.Include.NON_NULL) public class ResultVOimplements Serializable { private static final long serialVersionUID = -4414926978700453869L; private Integer code; private String msg; private T data; private Integer total; public ResultVO() { } public ResultVO(Integer code, String msg, T data, Integer total) { this.code = code; this.msg = msg; this.data = data; this.total = total; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public T getData() { return data; } public void setData(T data) { this.data = data; } public Integer getTotal() { return total; } public void setTotal(Integer total) { this.total = total; } @Override public String toString() { return "ResultVO{" + "code=" + code + ", msg='" + msg + ''' + ", data=" + data + ", total=" + total + '}'; } }
因为这个类需要经常使用,所以写了一个工具类不需要每次都创建它
package cn.gok.utils;
import cn.gok.common.ResultVO;
public class ResultUtils {
public static ResultVO successCodeMsg(String msg) {
ResultVO resultVO = new ResultVO<>();
resultVO.setCode(0);
resultVO.setMsg(msg);
resultVO.setData(null);
return resultVO;
}
public static ResultVO errorCodeMsg(String msg) {
ResultVO resultVO = new ResultVO<>();
resultVO.setCode(1);
resultVO.setMsg(msg);
resultVO.setData(null);
return resultVO;
}
public static ResultVO successDataCodeMsg(String msg,T data) {
ResultVO resultVO = new ResultVO<>();
resultVO.setCode(0);
resultVO.setMsg(msg);
resultVO.setData(data);
return resultVO;
}
public static ResultVO successDataCodeMsgTotal(String msg,T data,int total) {
ResultVO resultVO = new ResultVO<>();
resultVO.setCode(0);
resultVO.setMsg(msg);
resultVO.setData(data);
resultVO.setTotal(total);
return resultVO;
}
}
创建好再去对我们的controller进行修改,将数据进行封装到ResultVO中。
@RequestMapping(value = "/findAll",method = RequestMethod.POST)
@ResponseBody
public ResultVO
至此这个问题完美解决。
写代码不知道的不要一直闷着头去看自己的代码,自己去想为啥不正确,要换个思路,面向百度,面向文档去开发。
这是使用layui其中的一个要注意的点,还有遇到的问题我会去记录下来,方便下次自己在遇到不知道怎么去解决。



