How to return different view or output such as an Excel file, a PDF file, or a JSON object?
SpringMVC uses jsp as the default view, but you can always configure certain url to return a different view such as an Excel file, a PDF file, or JSON object. There are generally three parts in setting up a different view. First, create a controller that will listen to the specific url which you wish to return a different view. Second, configureweb/WEB-INF/config/view.xml
and map the different view to a view builder class. Finally, add the appropriate third-party libraries to your web application, if necessary.
Assuming we want all URL student.xls to return information about a student in Excel. First create a controller that will listen to the URL "student.xls".
@RequestMapping(value="student.xls") public ModelAndView getStudentExcel(@RequestParam(value="id") String id, ModelMap model) { ... model.addAttribute("student", student); return new ModelAndView("studentExcelView", model); }
Now add extra setting to map url ending with .xls to be handled by Spring MVC's controller in web.xml
.
<servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>*.xls</url-pattern> </servlet-mapping>
Next configure the web/WEB-INF/config/view.xml
to map "studentExcelView"
view name to a particular class that'll handle building the view.
<bean name="studentExcelView" class="edu.ucsd.act.hello.view.StudentExcelView"/>
Last step is to implement StudentExcelView class. We will utilize heavily on Spring's build-in support for Excel view and Apache's POI library to build an Excel document.
import org.apache.poi.hssf.*; import org.springframework.web.servlet.view.document.AbstractExcelView; public class StudentExcelView extends AbstractExcelView { ...
Following the same concept as an Excel view, a PDF view has the same setup except the view builder class uses Spring's build-in support for PDF view and iText to build a PDF document. Create a controller that'll handle the URL request
@RequestMapping(value="student.pdf") public ModelAndView getStudentPdf(@ReqeustParam(value="id"), ModelMap model) { ... model.addAttribute("student", student); return new ModelAndView("studentPdfView", model); }
Now add extra setting to map url ending with .pdf
to be handled by Spring MVC's controller in web.xml
.
<servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>*.pdf</url-pattern> </servlet-mapping>
The web/WEB-INF/config/view.xml
will map "studentPdfView"
view name toedu.ucsd.act.hello.view.StudentPdfView
class.
<bean name="studentPdfView" class="edu.ucsd.act.hello.view.StudentPdfView"/>
And the actual StudentPdfView
class will look something like this:
import com.lowagie.text.*; import org.springframework.web.servlet.view.document.AbstractPdfView; public class StudentExcelView extends AbstractPdfView { ...
Following the same concept as an Excel view, JSON will map a URL to a view builder class:
@RequestMapping(value="student.json") public ModelAndView getStudentJson(@ReqeustParam(value="id"), ModelMap model) { ... model.addAttribute("student", student); return new ModelAndView("studentJsonView", model); }
Now add extra setting to map url ending with .json
to be handled by Spring MVC's controller in web.xml
.
<servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>*.json</url-pattern> </servlet-mapping>
And the view will have the following mapping info in web/WEB-INF/config/view.xml
.
<bean name="studentJsonView" class="org.springframework.web.servlet.view.json.JsonView"/>
Despite what the package name suggests, this JsonView class is actually not part of the Spring framework. It is a separate open source package called Spring-Json View. And it will map any object or collection of objects into JSON format. So in this case, you don't even need to create a view builder class. Spring-Json View depends on SOJO.