ajax 返回前台数据

时间:2022-11-15 08:14:24

之前项目开发中,前台访问后台,后台往前台返回值,都是用@RequestBody 注解往前台返回值,并且返回数据类型不一样,方法的定义返回类型也不一样。后面用到了直接用ajax返回值到前台。觉得很方便,在此做下记录


package com.magingunion.framework.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;


public class AJAXUtil {

    /** * 返回前台数据(任何类型都可以,String、list等) * * @param response * @param jsonStr */
    public static void success(HttpServletResponse response, String jsonStr) {
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.write(jsonStr);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            if(out!=null) out.close();
        }
    }

    /** * 返回失败状态 * * @param response * @param jsonStr */
    public static void failure(HttpServletResponse response, String jsonStr) {
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        response.setStatus(500);
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.write(jsonStr);
            out.flush();
            out.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally{
            if(out!=null) out.close();
        }
    }

    /** * 返回json格式的msg * * @param bool * @param response Http response */
    public static void handleSuccess(HttpServletResponse response, Boolean bool) {
        PrintWriter out = null;
        String jsonObj = null;
        try {
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            out = response.getWriter();
            if (bool)
                jsonObj = "{\"msg\":\"SUCCESS\"}";
            else
                jsonObj = "{\"msg\":\"FAIL\"}";
        } catch (Exception e) {
            e.printStackTrace();
            jsonObj = "{\"msg\":\"FAIL\"}";
        }
        out.write(jsonObj.toString());
        out.flush();
        out.close();
    }

    /** * 返回自定义的msg * * @param bool * @param response Http response * @param msg */
    public static void handleSuccess(HttpServletResponse response,
            Boolean bool, String msg) {
        PrintWriter out = null;
        String jsonObj = null;
        try {
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            out = response.getWriter();
            if (bool)
                jsonObj = "{\"msg\":\"SUCCESS\",";
            else
                jsonObj = "{\"msg\":\"FAIL\",";

            jsonObj = jsonObj + msg + "}";
        } catch (Exception e) {
            e.printStackTrace();
            jsonObj = "{\"msg\":\"FAIL\"}";
        }
        out.write(jsonObj.toString());
        out.flush();
        out.close();
    }

}

在方法中,统一用void修饰。并且不需要使用@RequestBody注解,如果返回的是具体数据则用这个返回

AJAXUtil.success(response, JSONObject.toJSONString(data));

如果返回的是成功或者失败,则用

AJAXUtil.handleSuccess(response, true);

AJAXUtil.handleSuccess(response, false);