所介绍的工具包均可直接新建工具类丢在包下面。
1.在使用Controller向Ajax传参时常常使用JSON来完成传参,所以写了一个工具类,将返回的数据封装成JSON格式。
1 package com.lgqrlchinese.projectName.utils; 2 3 public class ResponseResult<T> { 4 5 public ResponseResult() { 6 7 } 8 9 public ResponseResult(T rows) { 10 this.data = rows; 11 } 12 13 public ResponseResult(String errorMessage) { 14 this.success = false; 15 this.message = errorMessage; 16 } 17 18 public ResponseResult(String errorCode, String errorMessage) { 19 this.success = false; 20 this.code = errorCode; 21 this.message = errorMessage; 22 } 23 24 private String code; // 返回的错误状态码 25 26 public void setCode(String code) { 27 this.code = code; 28 } 29 30 public String getCode() { 31 return this.code; 32 } 33 34 private Boolean success = true; 35 36 public Boolean setSuccess(Boolean success) { 37 return this.success = success; 38 } 39 40 public Boolean getSuccess() { 41 return this.success; 42 } 43 44 private T data; 45 46 public T getData() { 47 return data; 48 } 49 50 public void setData(T data) { 51 this.data = data; 52 } 53 54 private String message; 55 56 public String getMessage() { 57 return this.message; 58 } 59 60 public void setMessage(String message) { 61 this.success = false; 62 this.message = message; 63 } 64 65 }
使用Demo:
1 //这里只贴方法内容 2 public ResponseResult<List<Bean>> getAllUser(HttpServletResponse response) { 3 ResponseResult<List<Bean>> result = new ResponseResult<List<Bean>>(); 4 List<Bean> list = new ArrayList<Bean>(); 5 orderInfoManageService.selectAllOrder(); 6 list = orderInfoManageService.selectAllOrder(); 7 // result.setData(); 8 System.out.println("这是User列表"); 9 result.setData(list); 10 return result; 11 }