ChenJiaHe
2021-04-19 6efc676c6599cee0f82989fbda0e6bbdd71b9bfe
提交 | 用户 | age
5c5945 1 package com.hx.util;
E 2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5
6 import javax.servlet.http.HttpServletResponse;
7
8 import org.springframework.web.context.request.RequestContextHolder;
9 import org.springframework.web.context.request.ServletRequestAttributes;
10
11 import com.alibaba.fastjson.JSONObject;
12
13 /**
14  * 
15  * @author lijietian
16  * 
17  */
18 public class WebUtil {
19     /**
20      * 返回数据给前端的方法
21      * 
22      * @param msg 返回数据
23      *       
24      */
25     public static void AJAXdata(String msg) {
26         ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
27         HttpServletResponse response = attributes.getResponse();
28         
29         response.setContentType("text/plain");// 文本
30         response.setHeader("Cache-Control", "no-store");
31         response.setCharacterEncoding("utf-8");
32         try {
33             PrintWriter pw = response.getWriter();
34             pw.write(msg);
35             pw.flush();
36         } catch (IOException e) {
37             e.printStackTrace();
38         }
39     }
40     
41     /**
42      * 组成统一的格式
43      * 
44      */
45     public static String AJAXMsg(Integer status,String errorMsg,JSONObject obj) {
46         JSONObject info = new JSONObject();
47         info.put("status", status);
48         info.put("errorMsg", errorMsg);
49         info.put("datas", obj);
50         return info.toJSONString();
51     }
52     
53 }