layui-table-render 表格重载 500错误 中文乱码等


最近做layui-表格数据的时候发现遇到不少坑

第一:加载findAll所有数据的时候 返回json数据到网页上是'?' 

 

 

 @RequestMapping("/findAll")
    @ResponseBody
    public String findAll(int page, int limit){
        int start =(page-1)*limit;
        List<User> userList = userService.findAll(start, limit);

        int count = userService.getCount();

        /*HashMap<String, Object> map = new HashMap<String,Object>();
        map.put("code",0);
        map.put("count",count);
        map.put("data",userList);*/
         //用Json类封装前台中文都是?
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("count",count);
        jsonObject.put("data",userList);
        System.out.println(jsonObject.toString());
        //System.out.println("findAll-->map:"+map);
        return jsonObject.toString();
    }

解决 返回Map类型 中文类型就能正常显示。

@RequestMapping("/findAll")
    @ResponseBody
    public Map findAll(int page, int limit){
        int start =(page-1)*limit;
        List<User> userList = userService.findAll(start, limit);

        int count = userService.getCount();

        HashMap<String, Object> map = new HashMap<String,Object>();
        map.put("code",0);
        map.put("count",count);
        map.put("data",userList);
         //用Json类封装前台中文都是?
        /*JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("count",count);
        jsonObject.put("data",userList);
        System.out.println(jsonObject.toString());*/
        System.out.println("findAll-->map:"+map);
        return map;
    }

 

第二:表格重载的时候,Optional int parameter 'page' is present but cannot be translated into a null value due to being declared as a primitive type. 

原因:

 

 

 

 

 

 前台明明是有参数传入的,分页的page好像是默认自带的,最后找到是Controller层对应的方法有问题

@RequestMapping(value = "/selectByCondition",produces="text/html;charset=utf-8")
@ResponseBody
public Map selectByCondition(User user,int page, int limit){
System.out.println("selectByCondition执行了");
System.out.println("user:"+user);
System.out.println("page:"+page);
System.out.println("limit:"+limit);
int start =(page-1)*limit;
List<User> userList = userService.selectByCondition( user,start,limit);
System.out.println("userList:"+userList);
int count = userService.getCountByCondition(user);
System.out.println("count:"+count);
HashMap<String, Object> map = new HashMap<String,Object>();
System.out.println("map执行了吗?");
map.put("code",0);
map.put("count",count);
map.put("data",userList);
//用Json类封装前台中文都是?
/*JSONObject jsonObject = new JSONObject();
jsonObject.put("code",0);
jsonObject.put("count",count);
jsonObject.put("data",userList);
System.out.println(jsonObject.toString());*/
/*System.out.println("map:"+map);*/
return map;
}

把Map类型的返回值改成String类型返回json.toString就没问题了

 @RequestMapping(value = "/selectByCondition",produces="text/html;charset=utf-8")
    @ResponseBody
    public String selectByCondition(User user,int page, int limit){
        System.out.println("selectByCondition执行了");
        System.out.println("user:"+user);
        System.out.println("page:"+page);
        System.out.println("limit:"+limit);
        int start =(page-1)*limit;
        List<User> userList = userService.selectByCondition( user,start,limit);
        System.out.println("userList:"+userList);
        int count = userService.getCountByCondition(user);
        System.out.println("count:"+count);
        /*HashMap<String, Object> map = new HashMap<String,Object>();
        System.out.println("map执行了吗?");
        map.put("code",0);
        map.put("count",count);
        map.put("data",userList);*/
        //用Json类封装前台中文都是?
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("count",count);
        jsonObject.put("data",userList);
        System.out.println(jsonObject.toString());
        /*System.out.println("map:"+map);*/
        return jsonObject.toString();
    }

 

 

返回Json类型就没问题了 而且这次中文居然没有乱码 实在是amazing啊

 

 

 

 所以总结:分页的时候重载table.render的数据一定要是是Json格式的字符串,Map好像是不行的。

加载数据的时候Map和Json.toString都可以,而且Map没有乱码至少我这里是这样的 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM