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