最近做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沒有亂碼至少我這里是這樣的