原文連結:http://blog.csdn.net/qq_37936542/article/details/79064818
需求描述:ajax向後臺發起請求,springmvc在處理完請求後返回的結果型別不能夠統一,int/String/Map/List等等,這裡我自定義一個類,封裝返回結果
一:封裝主類 AjaxResponse
- package com.debo.common;
- import java.util.HashMap;
- import java.util.Map;
- public class AjaxResponse {
- private ReturnState returnState;
- private String returnMsg;
- private Map<String, Object> returnData = new HashMap<String, Object>();
- public AjaxResponse() {
- this(ReturnState.OK, "");
- }
- public AjaxResponse(String returnMsg) {
- this(ReturnState.OK, returnMsg);
- }
- public AjaxResponse(ReturnState returnState, String returnMsg) {
- this.returnState = returnState;
- this.returnMsg = returnMsg;
- }
- public AjaxResponse(String attributeName, Object attributeValue) {
- this();
- addAttribute(attributeName, attributeValue);
- }
- public static AjaxResponse getInstanceByResult(boolean result) {
- if (result) {
- return new AjaxResponse(Constant.OPERATE_SUCCESS);
- } else {
- return new AjaxResponse(ReturnState.ERROR, Constant.OPERATE_ERROR);
- }
- }
- public ReturnState getReturnState() {
- return returnState;
- }
- public void setReturnState(ReturnState returnState) {
- this.returnState = returnState;
- }
- public String getReturnMsg() {
- return returnMsg;
- }
- public void setReturnMsg(String returnMsg) {
- this.returnMsg = returnMsg;
- }
- public Map<String, Object> getReturnData() {
- if (returnData == null) {
- returnData = new HashMap<String, Object>();
- }
- return returnData;
- }
- public void setReturnData(Map<String, Object> returnData) {
- this.returnData = returnData;
- }
- public AjaxResponse addAttribute(String attributeName, Object attributeValue) {
- getReturnData().put(attributeName, attributeValue);
- return this;
- }
- }
二:返回狀態列舉 ReturnState
- package com.debo.common;
- /**
- * Ajax返回狀態
- *
- * @author lzg
- */
- public enum ReturnState {
- OK,
- WARNING,
- ERROR
- }
三:系統常用常量 Constant
- package com.debo.common;
- public class Constant {
- public static final int ZERO = 0;
- public static final String SUCCESS = "success";
- public static final String ERROR = "error";
- public static final String OPERATE_SUCCESS = "操作成功!";
- public static final String OPERATE_ERROR = "操作失敗!";
- public static final String NOT_PERMITTED = "許可權不足!";
- public static final String STATE1 = "1";
- public static final String STATE2 = "2";
- public static final int AFFECTED_LINE_0 = 0;
- public static final int AFFECTED_LINE_1 = 1;
- }
四:後臺測試程式碼
- package com.debo.common;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- @Controller
- public class Test {
- @RequestMapping("/test")
- @ResponseBody
- public AjaxResponse demo(){
- //操作正確時,返回正確資訊
- //return new AjaxResponse(ReturnState.OK,"操作成功");
- //return new AjaxResponse("操作成功");
- //return AjaxResponse.getInstanceByResult(true);
- //操作失敗,返回失敗資訊
- //return new AjaxResponse(ReturnState.ERROR,"登入名已存在");
- //return AjaxResponse.getInstanceByResult(false);
- //返回一個物件、集合或map
- //return new AjaxResponse("returnDate", map/List/物件);
- //返回多個物件、map、集合
- //AjaxResponse ajaxResponse = new AjaxResponse();
- //ajaxResponse.addAttribute("aaa", map);
- //ajaxResponse.addAttribute("bbb", list);
- //ajaxResponse.addAttribute("ccc", 物件);
- //預設操作成功,無返回資訊
- return new AjaxResponse();
- }
- }
- $.ajax({
- url : "test",
- type : "get",
- dataType : "json",
- success : function(data) {
- //返回狀態
- //alert(data.returnState);
- //返回資訊
- //alert(data.returnMsg)
- //返回的物件/集合/map
- alert(data.returnData.aaa)
- }
- });
文末福利:
福利一:前端,Java,產品經理,微信小程式,Python等8G資源合集大放送:https://www.jianshu.com/p/e8197d4d9880
福利二:微信小程式入門與實戰全套詳細視訊教程
領取方式:
如果需要學習視訊,歡迎關注 【程式設計微刊】微信公眾號,回覆【領取資源】一鍵領取以下所有乾貨資源,獲取更多有用技術乾貨、文件資料。所有文件會持續更新,歡迎關注一起成長!