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(); } }