전체소스 : spring304.zip

 

■ MappingJackJsonView : 자바 객체를 JSON으로 변환해서 보여주는 뷰 구현 클래스

  ● MappingJackJsonView 타입의 빈을 설정만 해주면 됨

    ▶ 별도의 LIB가 필요 (jackson-all-1.9.7.jar) : http://wiki.fasterxml.com/JacksonDownload

 

 

FileDownloadController.java

package sp.mvc.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

 

@Controller
public class FileDownloadController {


 @RequestMapping(value="/filedown.sp")
 public ModelAndView fileDownload(){
  System.out.println("----- FileDownloadController.fileDownload() -----");
  
  ModelAndView modelAndView = new ModelAndView("filedownload");
  return modelAndView;
 }
 
 
 @RequestMapping("/exceldown.sp")
 public String excelDownload(Model model){
  System.out.println("----- FileDownloadController.excelDownload() -----");
  
  List<String> list = new ArrayList<String>();
  list.add("사이트 관리");
  list.add("관리자 관리");
  list.add("공통 코드 관리");
  list.add("접속 이력");
  
  model.addAttribute("menuList", list);
  return "excelDownload";
 }
 
 
 @RequestMapping("/pdfdown.sp")
 public String pdfDownload(Model model){
  System.out.println("----- FileDownloadController.pdfDownload() -----");
  
  Map<String, String> map = new HashMap<String, String>();
  map.put("1", "cat");
  map.put("2", "dog");
  map.put("3", "tiger");
  
  model.addAttribute("animal", map);
  return "pdfDownload";
 }
 
 
 @RequestMapping("/jsondata.sp")
 public ModelAndView jsonData(){
  System.out.println("----- FileDownloadController.jsonData() -----");
  
  List<String> list = new ArrayList<String>();
  list.add("사이트 관리");
  list.add("관리자 관리");
  list.add("공통 코드 관리");
  list.add("접속 이력");
  
  Map<String, String> map = new HashMap<String, String>();
  map.put("1", "cat");
  map.put("2", "dog");
  map.put("3", "tiger");
  
  ModelAndView modelAndView = new ModelAndView();
  modelAndView.setViewName("jsonView");
  modelAndView.addObject("menuList", list);
  modelAndView.addObject("animal", map);
  return modelAndView;
 } 
}

 

main_config.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 

 <context:annotation-config/>
 <context:component-scan base-package="sp.mvc.controller"/>
 
 <!-- bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:alwaysUseFullPath="true"/ -->
 <!-- bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" p:alwaysUseFullPath="true"/ --> 
 
  
 <!-- ViewResolver 설정 -->
 <bean id="beanNameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="1"/>
 
 <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  p:order="2" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"></bean>


 
 <!-- 각종 bean 설정 -->
 <bean id="filedownload" class="sp.mvc.view.FileDownloadView"></bean>
 <bean id="excelDownload" class="sp.mvc.view.ExcelDownloadView"></bean>
 <bean id="pdfDownload" class="sp.mvc.view.PdfDownloadView"></bean>
 <bean id="jsonView" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"></bean>
 
</beans>

 

 

결과

요청 URL

   http://localhost:8090/spring304/jsondata.sp

 

----- FileDownloadController.jsonData() -----

 

 

 

 

[참고자료] Spring 3.0 프로그래밍-최범

+ Recent posts