460 likes | 656 Views
Spring MVC Views. Rendering custom views with Spring MVC. Topics in this Session. Revisiting Views Rendering PDF Documents Rendering Excel Spreadsheets Generating Jasper Reports Ajax Integration Internationalization. Topics in this Session. Revisiting Views Rendering PDF Documents
E N D
Spring MVC Views Rendering custom views with Spring MVC
Topics in this Session Revisiting Views Rendering PDF Documents Rendering Excel Spreadsheets Generating Jasper Reports Ajax Integration Internationalization
Topics in this Session Revisiting Views Rendering PDF Documents Rendering Excel Spreadsheets Generating Jasper Reports Ajax Integration Internationalization
Revisiting Views A View is a simple strategy interface Spring provides implementations for major view technologies You can write your own Easily unit tested using MockHttpServletRequest and MockHttpServletResponse
Revisiting ViewResolvers A ViewResolver is used by the DispatcherServlet to resolve logical view names to View implementations InternalResourceViewResolver resolves view names to a resource path (typically a JSP) BeanNameViewResolver resolves view names to the name of a Spring bean
View Integration Supported View Technologies JSP / JSTL Apache Velocity Freemarker Adobe PDF Microsoft Excel Jasper Reports XML / XSLT
View Independence Views are completely orthogonal to the Controller The keys within the Model form a contract The same Controller can be used to return different views of the same model HTML view PDF view Excel view
Topics in this Session Revisiting Views Rendering PDF Documents Rendering Excel Spreadsheets Generating Jasper Reports Ajax Integration Internationalization
Rendering PDF Documents The AbstractPdfView class integrates with iText for generating PDFs (see http://www.lowagie.com/iText) Provides a single abstract template method Contract with the Controller public class RewardsListPdfView extends AbstractPdfView { publicvoid buildPdfDocument(Map model, Document document, PdfWriter writer, HttpServletRequest request, HttpServletResponse response) throws Exception { List<Reward> rewards = (List<Reward>) model.get(“rewards”); for (Reward reward : rewards) { document.add(new Paragraph(“..”)); // … } } }
Configure the Existing Controller to use the PDF View No need to modify the RewardsController to support the new View Assuming the view name is parameterizable Define one configuration of the Controller for each View
Update the Servlet Application Context /WEB-INF/rewardsadmin-servlet-config.xml <beanid=“abstractRewardsListController” class=“rewardsadmin.RewardsListController” abstract=“true”> <constructor-arg ref=“rewardLookupService” /> </bean> <beanname=“/rewards/list.html” parent=“abstractRewardsListController”> <property name=“viewName” value=“list”/> </bean> <beanname=“/rewards/list.pdf” parent=“abstractRewardsListController”> <property name=“viewName” value=“listPdf”/> </bean>
Defining the PDF View RewardsListPdfView needs to be defined as a Spring bean The DispatcherServlet needs to be able to access the RewardsListPdfView bean The existing InternalResourceViewResolver resolves to JSPs Need to plug in an extra ViewResolver strategy to locate the PDF view Scales to handle any custom view implementations
Defining the View and ViewResolver <beanclass=“org.springframework.web.servlet.view. InternalResourceViewResolver”> …. </bean> <beanname=”listPdf” class=“rewardsadmin.RewardsListPdfView”/> <beanclass=“org.springframework.web.servlet.view. BeanNameViewResolver”> <property name=“order” value=“0”/> </bean> The existing ViewResolver The new View The new ViewResolver
Topics in this Session Revisiting Views Rendering PDF Documents Rendering Excel Spreadsheets Generating Jasper Reports Ajax Integration Internationalization
Rendering Excel Spreadsheets AbstractExcelView integrates with POI to generate Excel compatible Spreadsheets (see http://jakarta.apache.org/poi/index.html) Contract with the Controller public class RewardsListExcelView extends AbstractExcelView { publicvoid buildExcelDocument(Map model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception { List<Reward> rewards = (List<Reward>) model.get(“rewards”); HSSFSheet sheet = workbook.createSheet(“rewards”); for (int i = 0; i < rewards.size(); i++) { setText(getCell(sheet, i, 0), “..”); … } } }
Register the Controller with the DispatcherServlet <beanid=“abstractRewardsListController”> …. </bean> <beanname=“/rewards/list.xls” parent=“abstractRewardsListController”> <property name=“viewName” value=“listExcel”/> </bean> <bean name=“/rewards/list.html” parent=“abstractRewardsListController”> <property name=“viewName” value=“list”/> </bean> <beanname=“/rewards/list.pdf” parent=“abstractRewardsListController”> <property name=“viewName” value=“listPdf”/> </bean>
Defining the Excel View <beanname=“listExcel” class=“rewardsadmin.RewardsListExcelView”/> <beanname=”listPdf”class=“rewardsadmin.RewardsListPdfView”/> <beanclass=“org.springframework.web.servlet.view.InternalResourceViewResolver”> … </bean> <beanclass=“org.springframework.web.servlet.view.BeanNameViewResolver”> <property name=“order” value=“0”/> </bean> Resolves both ‘listPdf’ and ‘listExcel’
Topics in this Session Revisiting Views Rendering PDF Documents Rendering Excel Spreadsheets Generating Jasper Reports Ajax Integration Internationalization
Overview of Jasper Reports Jasper Reports is a very sophisticated document generator Capable of producing PDFs, Excel, HTML and CSV Report designs are expressed in a Jasper Reports XML format
Jasper Report Components (1) Reports Templates for rendering information about a model Sub Reports A report may contain one or more sub reports JRDataSource Source of the model that is used to populate a report
Jasper Reports Components (2) JRExporter Strategy class to render the report (HTML, PDF, etc.) .jrxml The XML report design .jasper The compiled XML report (generated by an Ant task or dynamically compiled at runtime)
Jasper Report Views Spring provides a number of Jasper Report views for rendering specific formats JasperReportsPdfView JasperReportsCsvView JasperReportsExcelView JasperReportsHtmlView Alternatively ConfigurableJasperReportsView allows you to provide your own JRExporter
The Contract when Using Jasper Reports The Model provides the contract between the Controller and the View The View implementation should not leak into the Controller For Jasper Reports, this contract can be quite complex Each report requires a JRDataSource One for each sub report
Specifying the JRDataSource Spring will automatically convert a java.util.Collection into a JRDataSource By default, it will use the first Collection in the Model Each sub report requires its own JRDataSource The name of the JRDataSource is configured in the master report.jrxml file This name will be used as a key in the model to locate either a JRDataSource or a Collection
Replacing the Pdf and Excel Views with Jasper Report Views The Report must be designed and stored in a .jrxml file /WEB-INF/jasper/rewards/list.jrxml Replace the RewardsListPdfView definition with the JasperReportsPdfView definition Replace the RewardsListExcelView definition with the JasperReportsExcelView definition
Update the View Definitions Contract with the Controller <beanid=“abstractJasperReportsListView”abstract=“true”> <property name=“url” value=“/WEB-INF/jasper/rewards/list.jrxml”/> <property name=“reportDataKey” value=“rewards”/> </bean> <beanname=“listPdf” parent=“abstractJasperReportsListView” class=“org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView”/> <beanname=“listExcel” parent=“abstractJasperReportsListView” class=“org.springframework.web.servlet.view.jasperreports.JasperReportsExcelView”/>
Topics in this Session Revisiting Views Rendering PDF Documents Rendering Excel Spreadsheets Generating Jasper Reports Ajax Integration Internationalization
Ajax Overview “Asynchronous JavaScript And Xml” Allows asynchronous refreshing of individual parts of a web page instead of the whole page Greatly improves user experience Reduces network traffic
Ajax Use Cases Typical use cases for Ajax include Form validation (validating the field as you type) Displaying potential results as you enter search criteria Adding new entries without refreshing the page The list is endless really; it is an enablingtechnology
Introduction to DWR Many different libraries and frameworks provide Ajax behavior Direct Web Remoting (DWR) integrates very well with Spring MVC Very popular and well established framework
Exposing Server Side Components DWR exposes server side components for invocation via JavaScript DWR creates a JavaScript proxy which will perform a RPC to the server side component DWR takes care of marshalling and sending your POJOs across the wire Ajax is asynchronous, so when invoking a server side method, a call-back must be registered
Integrating DWR with Spring MVC DWR requires its own Controller which handles all DWR requests Executes the RPC calls Dynamically creates a JavaScript file for each exposed service Each service should be exported by DWR
Configuring DWR on the Server Defaults to “/dwr” Name of the JavaScript file <beansxmlns=“http://www.springframework.org/schema/beans” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:dwr=“http://www.directwebremoting.org/schema/spring” xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd”> <dwr:controllerid=“dwrController”/> <beanid=“echoService” class=“example.EchoService”> <dwr:remote javascript=“EchoService”/> <property …/> </bean> </beans>
Configuring DWR on the Client DWR provides an engine.js JavaScript file which provides the DWR infrastructure DWR also generates one JavaScript file for each exported server side component The DWR engine Remote Service <scripttype=“test/javascript” src=“/dwr/engine.js”> </script> <scripttype=“test/javascript” src=“/dwr/interface/EchoService.js”> </script>
Using DWR on the Client Handling the response The remote invocation The Server Component API <html> <head> function callServer(message) { EchoService.echo(message, echoBack); } function echoBack(message) { document.getElementById(‘response’).value=‘Server said [’ + message + ‘]’; } </head> <body> <input type=“text”name=“prompt”onchange=“callServer(this.value)”/> <input type=“text”id=“response”/> </body> </html>
Walkthough of DWR Spring Application function callServer(message) { EchoService.echo(…); } Spring Application function echoBack(result) { document.getElement…. } Time
Topics in this Session Revisiting Views Rendering PDF Documents Rendering Excel Spreadsheets Generating Jasper Reports Ajax Integration Internationalization
Internationalization Overview The Internet is a worldwide entity, and web applications should take that into consideration Views should be rendered in the native language of the visitor The JDK supports internationalization through the use of MessageBundles and Locales The Locale represents the user’s Country and Language
Introduction to Resource Bundles ResourceBundles externalize all language sensitive strings Labels Messages Validation messages Each Localehas its own ResourceBundle using a well defined naming convention labels_en_GB.properties labels_en_US.properties etc.
How does Spring Support This? Spring provides a MessageSource Strategy interface to support message resolution There are two implementations, both support Java Resource Bundles (property files) ResourceBundleMessageSource ReloadableResourceBundleMessageSource
Defining a MessageSource <beans> <beanclass=“org.sfw.ResourceBundleMessageSource”> <propertyname=“basenames”> <list> <value>exceptions</value> <value>labels</value> </list> </property> </bean> </beans> Loads the resource bundles exceptions_XX_YY.properties and labels_XX_YY.properties from the classpath
Resolving Validation Error Keys When specifying validation errors, an error-key is specified (e.g. “error.age.too_young”) The error-key is the key in the resource bundle Spring will automatically resolve this with the registered resource bundles based on the current user’s Locale exceptions_en_GB.properties exceptions_en_US.properties etc.
Retrieving Locale Specific Labels You can also arbitrarily query the resource bundle messageSource.getMessage(key, args, locale); <spring:message key=“employee.age.label”/> The current Locale is determined by the defined LocaleResolver, and the appropriate resource bundle will be chosen labels_en_GB.properties labels_en_US.properties
Determining the Locale The mechanism of determining the current Locale is provided by a LocaleResolver public interface LocaleResolver { Locale getLocale(HttpServletRequest request); void setLocale(HttpServletRequest request, HttpServletReponse response, Locale locale); }
Defining a LocaleResolver Spring MVC provides a number of LocaleResolvers AcceptHeaderLocaleResolver (default) CookieLocaleResolver SessionLocaleResolver Write your own Simply define it as a bean in the Servlet’s ApplicationContext <beans> <beanclass=“org.sfw.CookieLocaleResolver”/> </beans>
Summary Spring integrates with a wide range of best of breed view technologies Spring’s strict implementation of MVC enables multiple renditions of the same Model without impacting the Controller Ajax is a very powerful technology which is changing the way developers and users think about web applications DWR and Spring enable developers to build richer, more interactive web applications