전체소스 : spring304.zip

 

■ iText API를 이용해서 PDF문서를 생성할 수 있는 AbstractPdfView 클래스를 제공

  ● 별도의 LIB가 필요 (itext-2.1.7.jar)

 

 

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";
 }
}

 

PdfDownloadView.java

package sp.mvc.view;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.view.document.AbstractPdfView;

import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

 

public class PdfDownloadView extends AbstractPdfView {

 

 @Override
 protected void buildPdfDocument(Map<String, Object> model, Document doc,
   PdfWriter writer, HttpServletRequest req, HttpServletResponse res)
   throws Exception {
  // TODO Auto-generated method stub
  System.out.println("---- PdfDownloadView.buildPdfDocument() ----");
  
  String userAgent = req.getHeader("User-Agent");
  String fileName = "test.pdf";
  
  if(userAgent.indexOf("MSIE") > -1){
   fileName = URLEncoder.encode(fileName, "utf-8");
  }else{
   fileName = new String(fileName.getBytes("utf-8"), "iso-8859-1");
  }
  
  res.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";");
  res.setHeader("Content-Transfer-Encoding", "binary");
  
  Map<String, String> map = (HashMap<String, String>)model.get("animal");
  
  Table table = new Table(2, map.size()+1);
  table.setPadding(5);
  
  BaseFont baseFont = BaseFont.createFont("c:\\windows\\fonts\\batang.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  
  Font font = new Font(baseFont);
  Cell cell = new Cell(new Paragraph("order", font));
  cell.setHeader(true);
  table.addCell(cell);
  
  cell = new Cell(new Paragraph("page", font));
  cell.setHeader(true);
  table.addCell(cell);
  table.endHeaders();
  
  Set<String> set = map.keySet();
  Iterator<String> it = set.iterator();
  
  while(it.hasNext()){
   String key = it.next();
   System.out.println("key : " + key);
   System.out.println("value : " + map.get(key));
   table.addCell(key);
   table.addCell(map.get(key));
  }
  
  doc.add(table);
 }
}

 

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>
 
</beans>

 

결과

요청 URL

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

 

----- FileDownloadController.pdfDownload() -----

---- PdfDownloadView.buildPdfDocument() ----
key : 3
value : tiger
key : 2
value : dog
key : 1
value : cat

 

 

 

 

 

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

 

+ Recent posts