Spring3 MVC的最佳实践和理解(9)
个人学习参考所用,勿喷!
9.创建一个资源多种实现的Excel和PDF视图
这里一个用于生成特定日期的预订消息的摘要报告的控制器为类:
@Controller @RequestMapping("/reservationSummary*") public class ReservationSummaryController { private ReservationService reservationService; @Autowired public ReservationSummaryController(ReservationService reservationService) { this.reservationService = reservationService; } @RequestMapping(method = RequestMethod.GET) public String generateSummary( @RequestParam(required = true, value = "date") String selectedDate, Model model) { List<Reservation> reservations = java.util.Collections.emptyList(); try { Date summaryDate = new SimpleDateFormat("yyyy-MM-dd").parse(selectedDate); reservations = reservationService.findByDate(summaryDate); } catch (java.text.ParseException ex) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); ex.printStackTrace(pw); throw new ReservationWebException("Invalid date format for reservation summary",new Date(),sw.toString()); } model.addAttribute("reservations",reservations); return "reservationSummary"; } }
从上面代码可以明确的看出返回的是同一的逻辑视图"reservationSummary"。
9.1)创建Excel视图。
这里需要添加Apache POI程序库的支持:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.5-FINAL</version> <type>jar</type> <scope>compile</scope> </dependency>
创建Excel视图的处理类:
public class ExcelReservationSummary extends AbstractExcelView { @SuppressWarnings({ "rawtypes", "unchecked", "deprecation" }) protected void buildExcelDocument(Map model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception { List<Reservation> reservations = (List) model.get("reservations"); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); HSSFSheet sheet = workbook.createSheet(); HSSFRow header = sheet.createRow(0); header.createCell((short) 0).setCellValue("Court Name"); header.createCell((short) 1).setCellValue("Date"); header.createCell((short) 2).setCellValue("Hour"); header.createCell((short) 3).setCellValue("Player Name"); header.createCell((short) 4).setCellValue("Player Phone"); int rowNum = 1; for (Reservation reservation : reservations) { HSSFRow row = sheet.createRow(rowNum++); row.createCell((short) 0).setCellValue(reservation.getCourtName()); row.createCell((short) 1).setCellValue( dateFormat.format(reservation.getDate())); row.createCell((short) 2).setCellValue(reservation.getHour()); row.createCell((short) 3).setCellValue( reservation.getPlayer().getName()); row.createCell((short) 4).setCellValue( reservation.getPlayer().getPhone()); } } }
这里我们还需要在资源集文件中添加对Excel资源的处理方式(当然也可以以其它的方式进行注册,只需要完成逻辑视图和视图对象的映射即可):
reservationSummary.(class)=com.apress.springrecipes.court.web.view.ExcelReservationSummary
这里我们可以用http://localhost:7070/Spring3MVC/reservationSummary.xls?date=2011-07-31的方式来获取到需要excel视图资源,当然这个资源是下载到本地了的。
9.2)创建PDF视图。
这里需要添加iText程序库的支持:
<dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artifactId> <version>2.1.7</version> <type>jar</type> <scope>compile</scope> </dependency>
创建PDF视图处理类:
public class PdfReservationSummary extends AbstractPdfView { @SuppressWarnings({ "rawtypes", "unchecked" }) protected void buildPdfDocument(Map model, Document document, PdfWriter writer, HttpServletRequest request, HttpServletResponse response) throws Exception { List<Reservation> reservations = (List<Reservation>) model.get("reservations"); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); Table table = new Table(5); table.addCell("Court Name"); table.addCell("Date"); table.addCell("Hour"); table.addCell("Player Name"); table.addCell("Player Phone"); if(!reservations.isEmpty()) { for (Reservation reservation : reservations) { table.addCell(reservation.getCourtName()); table.addCell(dateFormat.format(reservation.getDate())); table.addCell(Integer.toString(reservation.getHour())); table.addCell(reservation.getPlayer().getName()); table.addCell(reservation.getPlayer().getPhone()); } } document.add(table); } }
这里我们还需要在 资源集 文件中添加对pdf资源的处理方式,并且不能和上面的Excel映射添加到同一个 资源集 中,因为同一个 资源集 中不能处在对相同的键值:
reservationSummary.(class)=com.apress.springrecipes.court.web.view.ExcelReservationSummary
这里我们可以用http://localhost:7070/Spring3MVC/reservationSummary.pdf?date=2011-07-31的方式来获取到需要pdf视图资源,当然这个资源是下载到本地了的。
--------------------------------------------------------------------------------------------------------
附件为系列文章的示例代码,eclipse3.7.1中运行通过。
参考:
juyon的blog:《spring3 MVC国际化支持之中文乱码》
Gary Mark等的书籍:《Spring Recipes》2ed