原文链接: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代码
- $.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
福利二:微信小程序入门与实战全套详细视频教程
领取方式:
如果需要学习视频,欢迎关注 【编程微刊】微信公众号,回复【领取资源】一键领取以下所有干货资源,获取更多有用技术干货、文档资料。所有文档会持续更新,欢迎关注一起成长!