AjaxResults.java 3.8 KB

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