ajax 프론트 데이터 되 돌리 기
                                            
 6639 단어  기술 대사
                    
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);이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Java_IO 흐름(스 트림)\#\#노드 흐름 은 특정한 데이터 소스 에서 데 이 터 를 읽 고 쓰기 시작 합 니 다.가장 기본 적 인 byte 읽 기와 쓰기 방법 만 제공 합 니 다.기능 이 간단 합 니 다.\#여과 흐름 은 반드시 저급 흐름...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.