java for interactive data acquisition of front and back platform in Layui

About Layui

Layui is a UI framework for back-end programmers with low learning cost. The Json data format interacts with the front and back, and is also quite suitable for single page development. Interested friends can have a look layui official website.

Data interaction between front and back of Layui

layui has its own set of specific data format interaction (which is very important). The parameter code: 0, msg: "", count: data size (int), data: "data List" is required. In general, we choose to encapsulate the return receiving class.
Layui foreground js request data
Where html code

<link rel="stylesheet" href="static/layui/css/layui.css" media="all" />
<script type="text/javascript" src="static/layui/layui.js"></script>
<table class="layui-hide" id="test" lay-filter="table"></table>

js code

layui.use(['form','layer','table'], function(){
          var table = layui.table
          ,form = layui.form,$=layui.$;

          table.render({
            elem: '#test'  //Bind table id
            ,url:'sys/menu/list'  //Data request path
            ,cellMinWidth: 80
            ,cols: [[
              {type:'numbers'}
              ,{field:'name', title:'Menu name'}
              ,{field:'parentName', title:'Parent menu name',width:150}
              ,{field:'url', title: 'Menu path'}
              ,{field:'perms', title: 'Menu permissions'}
              ,{field:'type', title:'type'}
              ,{field:'icon', title:'Icon'}
              ,{field:'orderNum', title:'sort'}
              ,{fixed: 'right',title: 'operation', width:180,      align:'center', toolbar: '#toolBar'}//For details of a toolbar, please refer to the official website of layui
            ]]
            ,page: true   //Turn on paging
            ,limit:10   //Default 10 pieces of data one page
            ,limits:[10,20,30,50]  //Data paging bar
            ,id: 'testReload'  
          });
});

java background code

    @RequestMapping("/list")
        @ResponseBody
        @RequiresPermissions("sys:menu:list")
        public Layui list(@RequestParam Map<String, Object> params){
            //Query list data
            Query query = new Query(params);
            List<SysMenuEntity> menuList = sysMenuService.queryList(query);
            int total = sysMenuService.queryTotal(query);
            PageUtils pageUtil = new PageUtils(menuList, total, query.getLimit(), query.getPage());
            return Layui.data(pageUtil.getTotalCount(), pageUtil.getList());
        }

Layui tool class code

public class Layui  extends HashMap<String, Object> {

    public static Layui data(Integer count,List<?> data){
        Layui r = new Layui();
        r.put("code", 0);
        r.put("msg", "");
        r.put("count", count);
        r.put("data", data);
        return r;
    }
}

PageUtils are optional here. You can encapsulate them by yourself

@Data
public class PageUtils implements Serializable {
    private static final long serialVersionUID = -1202716581589799959L;

    //Total records
    private int totalCount;
    //Records per page
    private int pageSize;
    //PageCount 
    private int totalPage;
    //Current pages
    private int currPage;
    //List data
    private List<?> list;

    /**
     * paging
     * @param list        List data
     * @param totalCount  Total records
     * @param pageSize    Records per page
     * @param currPage    Current pages
     */
    public PageUtils(List<?> list, int totalCount, int pageSize, int currPage) {
        this.list = list;
        this.totalCount = totalCount;
        this.pageSize = pageSize;
        this.currPage = currPage;
        this.totalPage = (int)Math.ceil((double)totalCount/pageSize);
    }
}

In a word, the final data format that Layui receives is

Tags: JSON Javascript Java

Posted on Sun, 03 May 2020 09:19:24 -0400 by h3ktlk