public class Result<T> implements Serializable {
private static final long serialVersionUID = 3637122497350396679L;
private boolean success;
private T data;
private String msg;
public Result() {
}
public Result(boolean success) {
this.success = success;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Result(boolean success, String msg) {
super();
this.success = success;
this.msg = msg;
}
public Result(boolean success, T data) {
super();
this.success = success;
this.data = data;
}
}
该类为前后台交互提供了非常大的便捷:
下面是前后台的ajax交互:
前台ajax代码:
$.ajax({
url: "<%=request.getContextPath()%>/supp/deleteSupp",
data : {supplierId:supplierId},
async: false, //请求是否异步,默认为异步,这也是ajax重要特性
type: "GET", //请求方式
success: function(data) {
var rs = eval('('+data+')');
flag = rs.success;
if(flag){
alert("删除成功!");
}
}
});
下面是后台java代码:
@RequestMapping("/deleteSupp")
@ResponseBody
public Result<String> deleteSupplier(HttpServletRequest request){
Result<String> rs = new Result<>();
String supplierId = request.getParameter("supplierId");
supplierService.deleteSupplierById(supplierId);
rs.setSuccess(true);
return rs;
}