Framework/Spring
[STS] Spring ajax json 예제/ STS에서 ajax 사용하기
나비와꽃기린
2016. 10. 6. 16:27
1. pom.xml에 jackson 디펜던시 추가
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
2. javascript에서 ajax 사용
$.ajax({
type:"POST",
url: '/sts/getBoardList', //context root가 sts
dataType: "json",
cache : false,
success : function(resData){
console.log(resData);
},
error : function(xhr, status, e){
}
});
3. controller 에 url 매핑
@RequestMapping(value = "/getBoardList")
public @ResponseBody Map getBoardList(Locale locale, Model model) {
/* TEST DATA 생성 */
List list = new ArrayList();
Map map1 = new HashMap();
map1.put("ITEM", "blouse");
map1.put("PRICE","1500000000");
map1.put("LINK", "blouse@naver.com");
Map map2 = new HashMap();
map2.put("ITEM", "Tshirt");
map2.put("PRICE","1000000000");
map2.put("LINK", "Tshirt@naver.com");
Map map3 = new HashMap();
map3.put("ITEM", "Jeans");
map3.put("PRICE","2500000000");
map3.put("LINK", "Jeans@naver.com");
list.add(0, map1);
list.add(1, map2);
list.add(2, map3);
Map result = new HashMap();
result.put("items", list);
return result;
}