AjaxResult返回消息封装类

  1 /**
  2  * 操作消息提醒
  3  * 
  4  * @author 
  5  */
  6 public class AjaxResult extends HashMap<String, Object>
  7 {
  8     private static final long serialVersionUID = 1L;
  9 
 10     public static final String CODE_TAG = "code";
 11 
 12     public static final String MSG_TAG = "msg";
 13 
 14     public static final String DATA_TAG = "data";
 15 
 16     /**
 17      * 状态类型
 18      */
 19     public enum Type
 20     {
 21         /** 成功 */
 22         SUCCESS(0),
 23         /** 警告 */
 24         WARN(301),
 25         /** 错误 */
 26         ERROR(500);
 27         private final int value;
 28 
 29         Type(int value)
 30         {
 31             this.value = value;
 32         }
 33 
 34         public int value()
 35         {
 36             return this.value;
 37         }
 38     }
 39 
 40     /**
 41      * 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。
 42      */
 43     public AjaxResult()
 44     {
 45     }
 46 
 47     /**
 48      * 初始化一个新创建的 AjaxResult 对象
 49      * 
 50      * @param type 状态类型
 51      * @param msg 返回内容
 52      */
 53     public AjaxResult(Type type, String msg)
 54     {
 55         super.put(CODE_TAG, type.value);
 56         super.put(MSG_TAG, msg);
 57     }
 58 
 59     /**
 60      * 初始化一个新创建的 AjaxResult 对象
 61      * 
 62      * @param type 状态类型
 63      * @param msg 返回内容
 64      * @param data 数据对象
 65      */
 66     public AjaxResult(Type type, String msg, Object data)
 67     {
 68         super.put(CODE_TAG, type.value);
 69         super.put(MSG_TAG, msg);
 70         if (StringUtils.isNotNull(data))
 71         {
 72             super.put(DATA_TAG, data);
 73         }
 74     }
 75 
 76     /**
 77      * 返回成功消息
 78      * 
 79      * @return 成功消息
 80      */
 81     public static AjaxResult success()
 82     {
 83         return AjaxResult.success("操作成功");
 84     }
 85 
 86     /**
 87      * 返回成功数据
 88      * 
 89      * @return 成功消息
 90      */
 91     public static AjaxResult success(Object data)
 92     {
 93         return AjaxResult.success("操作成功", data);
 94     }
 95 
 96     /**
 97      * 返回成功消息
 98      * 
 99      * @param msg 返回内容
100      * @return 成功消息
101      */
102     public static AjaxResult success(String msg)
103     {
104         return AjaxResult.success(msg, null);
105     }
106 
107     /**
108      * 返回成功消息
109      * 
110      * @param msg 返回内容
111      * @param data 数据对象
112      * @return 成功消息
113      */
114     public static AjaxResult success(String msg, Object data)
115     {
116         return new AjaxResult(Type.SUCCESS, msg, data);
117     }
118 
119     /**
120      * 返回警告消息
121      * 
122      * @param msg 返回内容
123      * @return 警告消息
124      */
125     public static AjaxResult warn(String msg)
126     {
127         return AjaxResult.warn(msg, null);
128     }
129 
130     /**
131      * 返回警告消息
132      * 
133      * @param msg 返回内容
134      * @param data 数据对象
135      * @return 警告消息
136      */
137     public static AjaxResult warn(String msg, Object data)
138     {
139         return new AjaxResult(Type.WARN, msg, data);
140     }
141 
142     /**
143      * 返回错误消息
144      * 
145      * @return
146      */
147     public static AjaxResult error()
148     {
149         return AjaxResult.error("操作失败");
150     }
151 
152     /**
153      * 返回错误消息
154      * 
155      * @param msg 返回内容
156      * @return 警告消息
157      */
158     public static AjaxResult error(String msg)
159     {
160         return AjaxResult.error(msg, null);
161     }
162 
163     /**
164      * 返回错误消息
165      * 
166      * @param msg 返回内容
167      * @param data 数据对象
168      * @return 警告消息
169      */
170     public static AjaxResult error(String msg, Object data)
171     {
172         return new AjaxResult(Type.ERROR, msg, data);
173     }
174 }

---------------------------------------------------------------------------------------------

 

 

posted @ 2025-06-13 10:19  左耳听风  阅读(27)  评论(0)    收藏  举报