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