웹 & 프레임워크

<비공개> ajax json 예제

늘근이 2014. 5. 14. 09:21

<%@

             page contentType="text/plain;charset=utf-8"

             import="board.*"

%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%      

             BoardSalesDAO dao = new BoardSalesDAO();

             int soldCount[] = dao.getSoldCount();

             request.setAttribute("soldCount", soldCount);

%>

 

 

{

             <c:choose>

                           <c:when test="${not empty soldCount }">

                           "status" : true,

                           "totals" : [

                                        <c:forEach items="${soldCount }" var="count">

                                        ${count}

                                        <c:if test="${not status.last }">

                                        ,

                                        </c:if>

                                        </c:forEach>

                           ]

                           </c:when>

                           <c:otherwise>

                           "status" : false;

                           </c:otherwise>

             </c:choose>

}

 

<%--이러한형식을 보내기 위해 위에처럼 jstl el 써서 --%>

{

 "status" : "ok",

 "totals" : [10,20,30 ]

}

 

{

"status" :false

 

}

 

 

 

그리고 js는 이렇게 구성한다


function getNewTotals() {

             $.get("getUpdatedBoardSales-ajax-db.jsp" , updatePage,"json");

}

 

function updatePage(jsonObj) {

            

             if(!jsonObj.status) {

                           alert("데이터 조회에 실패하였습니다");

                           return;

             }

            

var boardsSold = jsonObj.totals[0];

var bootsSold =  jsonObj.totals[1];

var bindingsSold =  jsonObj.totals[2];

 

            

 

 

}