jsp
<div id="starRatingList">
<button class="btn btn-primary starRatingList" type="button" id="starInsert">리뷰</button>
</div>
ajax
$(function () {
$(".starRatingList").on("click", function(e){
e.preventDefault();
$.ajax({
url: "starList.do",
type: "POST",
dataType: "json",
success: function(data) {
console.log(data);
var list = data.list;
var starRatingHTML = '';
for (var i = 0; i < list.length; i++) {
starRatingHTML += '<blockquote class="blockquote comment border-top-0 border-left-0 border-right-0 px-0 pt-0">';
starRatingHTML += '<div class="d-sm-flex align-items-center pb-2">';
starRatingHTML += '<h6 class="mb-0">' + list[i].member_id + '</h6>';
starRatingHTML += '<span class="d-none d-sm-inline mx-2 text-muted opacity-50">|</span>';
starRatingHTML += '<div class="star-rating">';
for (var j = 0; j < 5; j++) {
if (j < list[i].star_count) {
starRatingHTML += '<span class="star">★</span>';
} else {
starRatingHTML += '<span class="star">☆</span>';
}
}
starRatingHTML += '</div></div>';
starRatingHTML += '<p class="mb-0">' + list[i].review + '</p>';
starRatingHTML += '</blockquote>';
}
$('#starRatingList').html(starRatingHTML);
},
error : function(request, status, error) {
alert("code:"+request.status+"\n"+"message:"+request.responseText+"\n"+"error:"+error);
}
});
});
});
**action**
public class StarListAction implements Action {
@Override
public ActionForward execute(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException, Exception {
response.setContentType("application/json;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
starDAO dao = starDAO.getInstance();
JSONArray list = dao.starList();
JSONObject result = new JSONObject();
result.put("list", list);
PrintWriter out = response.getWriter();
out.print(result);
return null;
}
}
道
public JSONArray starList() {
JSONArray result = new JSONArray();
try {
openConn();
sql = "select * from star_review order by store_num";
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
JSONObject obj = new JSONObject();
obj.put("member_id", rs.getString("member_id"));
obj.put("review", rs.getString("review"));
obj.put("star_count", rs.getInt("star_count"));
result.put(obj);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeConn(rs, pstmt, con);
}
return result;
}