Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Spring MVC Views Rendering custom views.

Slides:



Advertisements
Similar presentations
Chapter 6 Server-side Programming: Java Servlets
Advertisements

Apache Struts Technology
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
DT228/3 Web Development WWW and Client server model.
G O B E Y O N D C O N V E N T I O N WORF: Developing DB2 UDB based Web Services on a Websphere Application Server Kris Van Thillo, ABIS Training & Consulting.
Object-Oriented Enterprise Application Development Tomcat 3.2 Configuration Last Updated: 03/30/2001.
28/1/2001 Seminar in Databases in the Internet Environment Introduction to J ava S erver P ages technology by Naomi Chen.
Fast Track to ColdFusion 9. Getting Started with ColdFusion Understanding Dynamic Web Pages ColdFusion Benchmark Introducing the ColdFusion Language Introducing.
Copyright W. Howden1 Lecture 19: Intro to O/O Components.
1 Java Server Pages Can web pages be created specially for each user? What part does Java play?
Apache Struts Technology A MVC Framework for Java Web Applications.
Session-01. Layers Struts 2 Framework The struts 2 framework is used to develop MVC-based web application. Struts 1.0 was released in June The.
Struts. Agenda Preface Struts and its components An example The architecture required for Struts Applications.
UNIT-V The MVC architecture and Struts Framework.
INTRODUCTION TO WEB DATABASE PROGRAMMING
JavaServer Faces: The Fundamentals Compiled from Sun TechDays workshops (JSF Basics, Web-Tier Codecamp: JavaServer Faces, Java Studio Creator; IBM RAD)
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
Introduction to the Enterprise Library. Sounds familiar? Writing a component to encapsulate data access Building a component that allows you to log errors.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
XForms: A case study Rajiv Shivane & Pavitar Singh.
ASP.NET + Ajax Jesper Tørresø ITNET2 F08. Ajax Ajax (Asynchronous JavaScript and XML) A group of interrelated web development techniques used for creating.
Building a UI with Zen Pat McGibbon –Sales Engineer.
Internationalization and the Java Stack Part 1 Matt Wheeler.
1 3. Implementing Web Services 1.Create SOAP proxy interfaces and WSDL based service descriptions 2.Register/publish services 3.Stores service descriptions.
Copyright © 2012 Accenture All Rights Reserved.Copyright © 2012 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are.
Internationalization and the Java Stack Matt Wheeler.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Understanding the Bean Lifecycle An.
Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some.
JSF Introduction Copyright © Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in print without written permission.
Internationalization in the Java Stack Matt Wheeler.
Lecturer: Prof. Piero Fraternali, Teaching Assistant: Alessandro Bozzon, Advanced Web Technologies: Struts–
JSTL Lec Umair©2006, All rights reserved JSTL (ni) Acronym of  JavaServer Pages Standard Tag Library JSTL (like JSP) is a specification, not an.
1 Geospatial and Business Intelligence Jean-Sébastien Turcotte Executive VP San Francisco - April 2007 Streamlining web mapping applications.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Forms with Spring MVC Handling Form.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Spring Remoting Simplifying.
JSF Framework Java Server Faces Presented by Songkran Totiya (6/10/2014)
Struts Framework Anna Paščenko. What is Struts?  An open source framework for building Java web applications.
Server-side Programming The combination of –HTML –JavaScript –DOM is sometimes referred to as Dynamic HTML (DHTML) Web pages that include scripting are.
Chính phủ điện tử TS. Phạm Văn Tính Khoa CNTT, ĐH Nông Lâm TP.HCM
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Spring MVC Essentials Getting started.
JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.
1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Testing Spring Applications Unit Testing.
Preface IIntroduction Objectives I-2 Course Overview I-3 1Oracle Application Development Framework Objectives 1-2 J2EE Platform 1-3 Benefits of the J2EE.
13 Copyright © 2004, Oracle. All rights reserved. Adding Validation and Error Handling.
Chapter 3 JSP Overview. The Problem with Servlets processing the request and generating the response are both handled by a single servlet class Java programming.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Data Access with Spring.
Modern Programming Language. Web Container & Web Applications Web applications are server side applications The most essential requirement.
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
.NET Mobile Application Development XML Web Services.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Overview of the Spring Framework Introducing.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Developing Rich Web Applications with.
Message Framework Topic subscribe for javascript/flex client.
10 Copyright © 2004, Oracle. All rights reserved. Building ADF View Components.
APACHE STRUTS ASHISH SINGH TOMAR ast2124. OUTLINE Introduction The Model-View-Controller Design Pattern Struts’ implementation of the MVC Pattern Additional.
Chapter 2 Java Struct 2. Content Basic MVC Architecture Struts 2 Overview Struts 2 - Architecture.
SDJ INFOSOFT PVT. LTD. 2 BROWSERBROWSER JSP JavaBean DB Req Res Application Layer Enterprise server/Data Sources.
Apache Struts Technology A MVC Framework for Java Web Applications.
Apache Cocoon – XML Publishing Framework 데이터베이스 연구실 박사 1 학기 이 세영.
A Presentation Presentation On JSP On JSP & Online Shopping Cart Online Shopping Cart.
Struts 2 Development. Topics  Roles in Struts Development  Control Flow  Actions  Struts 2 Views and Target  Struts 2 Custom Tags  Validation 
Office 365 Development July 2014.
Introduction to Servlets
Web Programming Developing Web Applications including Servlets, and Web Services using NetBeans 6.5 with GlassFish.V3.
Internationalization
Unit 6-Chapter 2 Struts.
JavaServer Faces: The Fundamentals
Presentation transcript:

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Spring MVC Views Rendering custom views with Spring MVC

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 2 Topics in this Session Revisiting Views Rendering PDF Documents Rendering Excel Spreadsheets Generating Jasper Reports Ajax Integration Internationalization

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 3 Topics in this Session Revisiting Views Rendering PDF Documents Rendering Excel Spreadsheets Generating Jasper Reports Ajax Integration Internationalization

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 4 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

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 5 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

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 6 View Integration Supported View Technologies –JSP / JSTL –Apache Velocity –Freemarker –Adobe PDF –Microsoft Excel –Jasper Reports –XML / XSLT

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 7 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

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 8 Topics in this Session Revisiting Views Rendering PDF Documents Rendering Excel Spreadsheets Generating Jasper Reports Ajax Integration Internationalization

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 9 Rendering PDF Documents The AbstractPdfView class integrates with iText for generating PDFs (see Provides a single abstract template method public class RewardsListPdfView extends AbstractPdfView { public void buildPdfDocument(Map model, Document document, PdfWriter writer, HttpServletRequest request, HttpServletResponse response) throws Exception { List rewards = (List ) model.get(“rewards”); for (Reward reward : rewards) { document.add(new Paragraph(“..”)); // … } public class RewardsListPdfView extends AbstractPdfView { public void buildPdfDocument(Map model, Document document, PdfWriter writer, HttpServletRequest request, HttpServletResponse response) throws Exception { List rewards = (List ) model.get(“rewards”); for (Reward reward : rewards) { document.add(new Paragraph(“..”)); // … } Contract with the Controller

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 10 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

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 11 Update the Servlet Application Context <bean id=“abstractRewardsListController” class=“rewardsadmin.RewardsListController” abstract=“true”> <bean id=“abstractRewardsListController” class=“rewardsadmin.RewardsListController” abstract=“true”> /WEB-INF/rewardsadmin-servlet-config.xml

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 12 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

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 13 Defining the View and ViewResolver <bean class=“org.springframework.web.servlet.view. InternalResourceViewResolver”> …. <bean name=”listPdf” class=“rewardsadmin.RewardsListPdfView”/> <bean class=“org.springframework.web.servlet.view. BeanNameViewResolver”> <bean class=“org.springframework.web.servlet.view. InternalResourceViewResolver”> …. <bean name=”listPdf” class=“rewardsadmin.RewardsListPdfView”/> <bean class=“org.springframework.web.servlet.view. BeanNameViewResolver”> The existing ViewResolver The new ViewThe new ViewResolver

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 14 Topics in this Session Revisiting Views Rendering PDF Documents Rendering Excel Spreadsheets Generating Jasper Reports Ajax Integration Internationalization

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 15 Rendering Excel Spreadsheets AbstractExcelView integrates with POI to generate Excel compatible Spreadsheets –(see public class RewardsListExcelView extends AbstractExcelView { public void buildExcelDocument(Map model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception { List rewards = (List ) model.get(“rewards”); HSSFSheet sheet = workbook.createSheet(“rewards”); for (int i = 0; i < rewards.size(); i++) { setText(getCell(sheet, i, 0), “..”); … } public class RewardsListExcelView extends AbstractExcelView { public void buildExcelDocument(Map model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception { List rewards = (List ) model.get(“rewards”); HSSFSheet sheet = workbook.createSheet(“rewards”); for (int i = 0; i < rewards.size(); i++) { setText(getCell(sheet, i, 0), “..”); … } Contract with the Controller

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 16 Register the Controller with the DispatcherServlet …. ….

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 17 Defining the Excel View <bean name=“listExcel” class=“rewardsadmin.RewardsListExcelView”/> … <bean name=“listExcel” class=“rewardsadmin.RewardsListExcelView”/> … Resolves both ‘listPdf’ and ‘listExcel’

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 18 Topics in this Session Revisiting Views Rendering PDF Documents Rendering Excel Spreadsheets Generating Jasper Reports Ajax Integration Internationalization

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 19 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

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 20 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

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 21 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)

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 22 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

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 23 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

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 24 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

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 25 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

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 26 Update the View Definitions <bean name=“listPdf” parent=“abstractJasperReportsListView” class=“org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView”/> <bean name=“listExcel” parent=“abstractJasperReportsListView” class=“org.springframework.web.servlet.view.jasperreports.JasperReportsExcelView”/> <bean name=“listPdf” parent=“abstractJasperReportsListView” class=“org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView”/> <bean name=“listExcel” parent=“abstractJasperReportsListView” class=“org.springframework.web.servlet.view.jasperreports.JasperReportsExcelView”/> Contract with the Controller

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 27 Topics in this Session Revisiting Views Rendering PDF Documents Rendering Excel Spreadsheets Generating Jasper Reports Ajax Integration Internationalization

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 28 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

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 29 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 enabling technology

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 30 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

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 31 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

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 32 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

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 33 Configuring DWR on the Server <beans xmlns=“ xmlns:xsi=“ xmlns:dwr=“ xsi:schemaLocation=" > <beans xmlns=“ xmlns:xsi=“ xmlns:dwr=“ xsi:schemaLocation=" > Defaults to “/dwr” Name of the JavaScript file

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 34 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

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 35 Using DWR on the Client function callServer(message) { EchoService.echo(message, echoBack); } function echoBack(message) { document.getElementById(‘response’).value=‘Server said [’ + message + ‘]’; } function callServer(message) { EchoService.echo(message, echoBack); } function echoBack(message) { document.getElementById(‘response’).value=‘Server said [’ + message + ‘]’; } The remote invocationHandling the response The Server Component API

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 36 Walkthough of DWR function callServer(message) { EchoService.echo(…); } Spring Application function echoBack(result) { document.getElement…. } Spring Application Time

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 37 Topics in this Session Revisiting Views Rendering PDF Documents Rendering Excel Spreadsheets Generating Jasper Reports Ajax Integration Internationalization

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 38 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

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 39 Introduction to Resource Bundles ResourceBundles externalize all language sensitive strings –Labels –Messages –Validation messages Each Locale has its own ResourceBundle using a well defined naming convention –labels_en_GB.properties –labels_en_US.properties –etc.

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 40 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

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 41 Defining a MessageSource exceptions labels exceptions labels Loads the resource bundles exceptions_XX_YY.properties and labels_XX_YY.properties from the classpath

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 42 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.

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 43 Retrieving Locale Specific Labels You can also arbitrarily query the resource bundle –messageSource.getMessage(key, args, locale); – 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

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 44 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); } public interface LocaleResolver { Locale getLocale(HttpServletRequest request); void setLocale(HttpServletRequest request, HttpServletReponse response, Locale locale); }

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 45 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

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 46 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