> Dmitry Sklyut > Matt Swartley. Copyright 2005 Chariot Solutions

Similar documents
Distributed Applications Spring MVC 2.5

Handling web requests

Developing Spring based WebSphere Portal application using IBM Rational Application Developer

Standards and the Portals Project

Portlet Standard JSR 168 / JSR 286

Spring MVC. Richard Paul Kiwiplan NZ Ltd 27 Mar 2009

Advanced Software Engineering

Pro Spring MVC. Marten Deinum Koen Serneels. With Web Flow. Spring Web Flow project founder. Foreword by Erwin Vervaet,

Advanced Web Systems 4- PORTLET API specifications (JSR 286) A. Venturini

Portlets (JSR-168) Dave Landers. BEA Systems, Inc. Dave Landers Portlets (JSR-168)

Java EE Application Assembly & Deployment Packaging Applications, Java EE modules. Model View Controller (MVC)2 Architecture & Packaging EJB Module

Annotation-Based Spring Portlet MVC

Spring MVC. PA 165, Lecture 8. Martin Kuba

JVA-117A. Spring-MVC Web Applications

Getting Data from the User with Forms. A Basic Form-Submission Workflow

Struts: Struts 1.x. Introduction. Enterprise Application

Index. Combined lifecycle strategy, annotation, 93 ContentNegotiatingViewResolver, 78

G l a r i m y Training on. Spring Framework

Java Advance Frameworks

JAVA COURSES. Empowering Innovation. DN InfoTech Pvt. Ltd. H-151, Sector 63, Noida, UP

Java J Course Outline

Call: JSP Spring Hibernate Webservice Course Content:35-40hours Course Outline

Mastering Spring MVC 3

Sang Shin. Java Portlets (JSR-168) Revision History. Disclaimer & Acknowledgments

Liferay 7 Portlet to Empower Your Custom Development

Implementing JSR 168 inter-portlet communication using Rational Application Developer V6.0 and WebSphere Portal V5.1

~$> whoami. Ioan Marius Curelariu SDE at Website Analytics Amazon Development Center Romania

PORTIONS (PORTlet actions) User Guide

Introduction to Spring Framework: Hibernate, Spring MVC & REST

JSR-286: Portlet Specification 2.0

Building Web Applications With The Struts Framework

Enterprise Integration Using Dependency Injection

Web Presentation Patterns (controller) SWEN-343 From Fowler, Patterns of Enterprise Application Architecture

Oracle Developer Day

5.1 Registration and Configuration

Liferay Faces. Reference Documentation ga4

Strecks Java 5 Extensions

Oracle Developer Day

CS506 Web Design & Development Final Term Solved MCQs with Reference

JSR-286: Portlet Specification 2.0

Content Server / Spark. Version: 6.3. Delivery Portlet Developer s Guide

Specialized - Mastering Spring 4.2

Specialized - Mastering JEE 7 Web Application Development

2005, Cornell University

Call: Core&Advanced Java Springframeworks Course Content:35-40hours Course Outline

Java EE 6: Develop Web Applications with JSF

IBM Realtests LOT-911 Exam Questions & Answers

Contents at a Glance

Table of Contents. Introduction... xxi

J2EE - Version: 25. Developing Enterprise Applications with J2EE Enterprise Technologies

WA2089 WebSphere Portal 8.0 Programming EVALUATION ONLY

FINALTERM EXAMINATION Spring 2009 CS506- Web Design and Development Solved by Tahseen Anwar

Spring & Hibernate. Knowledge of database. And basic Knowledge of web application development. Module 1: Spring Basics

Building Blocks for a Simple TeraGrid Science Gateway

Index. NOTE: Boldface numbers indicate illustrations or code listing; t indicates a table. 281

SOFTWARE DEVELOPMENT SERVICES WEB APPLICATION PORTAL (WAP) TRAINING. Intuit 2007

The Struts MVC Design. Sample Content

Session 25. Spring Controller. Reading & Reference

Spring Integration on SAP NetWeaver

Java EE 7: Back-End Server Application Development

Fast Track to Spring 3 and Spring MVC / Web Flow

The 60-Minute Guide to Development Tools for IBM Lotus Domino, IBM WebSphere Portal, and IBM Workplace Applications

ADVANCED JAVA TRAINING IN BANGALORE

Tutorial: Developing a Simple Hello World Portlet

Page 1

Writing Servlets and JSPs p. 1 Writing a Servlet p. 1 Writing a JSP p. 7 Compiling a Servlet p. 10 Packaging Servlets and JSPs p.

Spring in Action. Craig Walls. Spring Dallas User Group August 15, 2007 These slides:

What's New in the Servlet and JavaServer Pages Technologies?

FreeMarker in Spring Web. Marin Kalapać

COURSE 9 DESIGN PATTERNS

Implementing a Numerical Data Access Service

JavaServer Faces Technology, AJAX, and Portlets: It s Easy if You Know How!

Spring Framework 2.5: New and Notable. Ben Alex, Principal Software Engineer, SpringSource

Fast Track to Java EE

Distributed Multitiered Application

SAS Web Infrastructure Kit 1.0. Developer s Guide

Teamcenter Global Services Customization Guide. Publication Number PLM00091 J

Skyway Builder 6.3 Reference

JVA-563. Developing RESTful Services in Java

Liferay Faces. Reference Documentation ga2

Oracle - Developing Applications for the Java EE 7 Platform Ed 1 (Training On Demand)

Introduction to Web Application Development Using JEE, Frameworks, Web Services and AJAX

SSC - Web development Model-View-Controller for Java Servlet

Advanced Web Systems 3- Portlet and JSP-JSTL. A. Venturini

Introduction to JSP and Servlets Training 5-days

Developing Applications with Java EE 6 on WebLogic Server 12c

COURSE DETAILS: CORE AND ADVANCE JAVA Core Java

IBM Rational Application Developer for WebSphere Software, Version 7.0

Introduction to Spring Framework: Hibernate, Web MVC & REST

Java- EE Web Application Development with Enterprise JavaBeans and Web Services

J2EE Interview Questions

JVA-117E. Developing RESTful Services with Spring

Lotus Exam IBM Websphere Portal 6.1 Application Development Version: 5.0 [ Total Questions: 150 ]

Magnolia Community Edition vs. Enterprise Edition. Non-Functional Features. Magnolia EE. Magnolia CE. Topic. Good value for money.

Developing JSR-168- Compliant Portlets for the SAS Information Delivery Portal 4.2

JavaEE Interview Prep

"Charting the Course... WebSphere Portal 8 Development using Rational Application Developer 8.5. Course Summary

Session 24. Introduction to Java Server Faces (JSF) Robert Kelly, Reading.

Introduction to Seam. Pete Muir. JBoss, a division of Red Hat

CMP 436/774. Introduction to Java Enterprise Edition. Java Enterprise Edition

Transcription:

Introduction to Spring MVC > Dmitry Sklyut > Matt Swartley Copyright 2005 Chariot Solutions

About Chariot Solutions Small, high-powered consulting firm Focused on Java and open source Services include: Architecture: Strategy, infrastructure planning, technology selection, enterprise and application architecture Development: Full lifecycle application development, integration of existing systems, extension and/or rehabilitation of existing Java applications Rapid Response: Quick resolutions to time-critical issues (performance and stability problems, missing features, critical defects, security flaws...) If you don t work for us, you should! We're hiring! Email: emulder@chariotsolutions.com 2 Copyright 2005 Chariot Solutions

Agenda Overview of Spring MVC Request Lifecycle Form Processing New Spring MVC Features in Spring 2.0 Overview of JSR-168 Portlet Request Lifecycle Spring Web MVC vs. Spring Portlet MVC 3 Copyright 2005 Chariot Solutions

Copyright 2005 Chariot Solutions Overview of Spring MVC

Overview Spring MVC is a framework that simplifies development of the Web Tier Simplifies testing through dependency injection Simplifies binding request data to domain objects Simplifies form validation and error handling Simplifies implementation of multiple view technologies (JSP, Velocity, Excel, PDF, ) Simplifies multiple page workflow 5 Copyright 2005 Chariot Solutions

Copyright 2005 Chariot Solutions Request Lifecycle

Request Lifecycle 2 Url Handler Mapping 1 Dispatcher Servlet ModelAndView 3 4 Controller 6 5 ViewResolver View 7 Copyright 2005 Chariot Solutions

DispatcherServlet Spring MVC s front controller Coordinates the entire request lifecycle Configured in web.xml <servlet> <servlet-name>beer</servlet-name> <servlet-class> org.springframework.web.servlet.dispatcherservlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> Loads Spring application context from XML file (default is <servlet-name>-servlet.xml) that usually contains <bean> definitions for the Spring MVC components 8 Copyright 2005 Chariot Solutions

Url Handler Mappings Tell the DispatcherServlet which controller to invoke for a request Implement the HandlerMapping interface Spring provides several implementations BeanNameUrlHandlerMapping <bean name="/index.htm" class="beer.controllers.welcomecontroller"> <bean name="/managebeers.htm" class="beer.controllers.managebeerscontroller"> <bean name="/editbeer.htm" class="beer.controllers.beerformcontroller"> 9 Copyright 2005 Chariot Solutions

Url Handler Mappings (contd.) SimpleUrlHandlerMapping <bean id="urlmapping" class="org.sfw.web.servlet.handler.simpleurlhandlermapping"> <property name="mappings"> <props> <prop key="/index.htm">welcomecontroller</prop> <prop key="/managebeers.htm">managebeerscontroller</prop> <prop key="/editbeer.htm">beerformcontroller</prop> </props> </property> </bean> CommonsPathHandlerMapping Uses source level meta-data (a PathMap attribute) compiled into the controller using the Jakarta Commons Atttributes compiler 10 Copyright 2005 Chariot Solutions

Controllers Receive requests from DispatcherServlet and coordinate business functionality Implement the Controller interface public ModelAndView handlerequest(httpservletrequest request, HttpServletResponse response) throws Exception; Return instance of ModelAndView to DispatcherServlet ModelAndView contains the model (a Map) and either a logical view name, or an implementation of the View interface 11 Copyright 2005 Chariot Solutions

Controllers (contd.) Spring provides many implementations: AbstractController AbstractCommandController ParameterizableViewController MultiActionController SimpleFormController AbstractWizardController Can handle requests as commands (as in WebWork) with the ThrowawayController 12 Copyright 2005 Chariot Solutions

View Resolvers Resolve logical view names returned from controllers into View objects Implement the ViewResolver interface Spring provides several implementations 13 Copyright 2005 Chariot Solutions

View Resolvers (contd.) InternalResourceViewResolver <bean id="viewresolver" class="org.sfw.web.servlet.view.internalresourceviewresolver"> </bean> <property name="viewclass"> <value>org.springframework.web.servlet.view.jstlview</value> </property> <property name="prefix"> <value>/web-inf/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> 14 Copyright 2005 Chariot Solutions

View Resolvers (contd.) BeanNameViewResolver <bean id="viewresolver" class="org.sfw.web.servlet.view.beannameviewresolver"/> <bean id="managebeers" class="org.springframework.web.servlet.view.jstlview"> </bean> <property name="url"> <value>/web-inf/jsp/managebeers.jsp</value> </property> <bean id="managebeersasexcel" class="beer.views.managebeersexcelview"/> 15 Copyright 2005 Chariot Solutions

View Resolvers (contd.) ResourceBundleViewResolver <bean id="viewresolver" class="org.sfw.web.servlet.view.resourcebundleviewresolver"> </bean> <property name="basename"><value>views</value></property> views.properties welcome.(class)=org.springframework.web.servlet.view.jstlview welcome.url=/web-inf/jsp/welcome.jsp managebeers.(class)=org.springframework.web.servlet.view.jstlview managebeers.url=/web-inf/jsp/managebeers.jsp managebeersasexcel.(class)=beer.views.managebeersexcelview 16 Copyright 2005 Chariot Solutions

View Resolvers (contd.) XmlViewResolver <bean id="viewresolver" class="org.sfw.web.servlet.view.xmlviewresolver"> </bean> <property name="location"><value>views</value></property> views.xml <bean name="welcome" class="org.springframework.web.servlet.view.jstlview"> <property name="url"> <value>/web-inf/jsp/welcome.jsp</value> </property> </bean> 17 Copyright 2005 Chariot Solutions

Views Renders the output of the request to the client Implement the View interface Built in support for: JSP, XSLT, Velocity, Freemarker Excel, PDF, JasperReports And of course, you can write your own custom implementation 18 Copyright 2005 Chariot Solutions

Copyright 2005 Chariot Solutions Sample Application (Part 1)

Copyright 2005 Chariot Solutions Form Processing

Form Processing SimpleFormController handles single page form input AbstractWizardFormController handles multi-page form input Flexible Binding System Many default binders (Date, Number, etc.) Easy to create custom binders (PropertyEditor implementations) Binding support (in Views) for JSP, Velocity and FreeMarker Flexible Validation Framework Injected into Controllers Applied automatically during form processing Text of error messages resolved from ResourceBundle Field level error information available in Views 21 Copyright 2005 Chariot Solutions

Some Definitions Form Backing Object The object being modified by the form Can be any POJO Typically a class created for a specific form or a domain object Sometimes called the Command object Reference Data Supporting (read-only) data needed to render the form Often used to populate drop down lists 22 Copyright 2005 Chariot Solutions

SimpleFormController Split into two distinct workflows Form Request Load form backing object and reference data Show form view Form Submission Load form backing object Bind and validate form backing object (show form view on error) Execute submission logic Show success view 23 Copyright 2005 Chariot Solutions

SimpleFormController Form View Controller receives a request for a new form (typically a GET) formbackingobject() Allows you to load or create the object edited by the form initbinder() Allows you to register custom editors for fields in the command object showform() Returns the View to be rendered referencedata() Allows you to add data needed by the form (select lists, etc.) to the model Model gets exposed and view gets rendered 24 Copyright 2005 Chariot Solutions

SimpleFormController Form Submission Controller receives a form submission (typically a POST) formbackingobject() Allows you to load or create the object edited by the form Request data is bound to the form backing object onbind() Allows you to do custom processing after binding but before validation Validator is invoked onbindandvalidate() Allows you to do custom processing after binding and validation onsubmit()- Allows you to do custom submission processing 25 Copyright 2005 Chariot Solutions

Custom Binding Spring uses PropertyEditors to bind request data to form backing object Provides a convenient superclass class called PropertyEditorSupport for custom property editors Need to provide implementations for setastext(string text) and getastext() 26 Copyright 2005 Chariot Solutions

Validators Used to validate POJOs typically form objects and domain objects Usually injected into controllers for validation during form processing Implement the Validator interface boolean supports(class clazz); void validate(object obj, Errors errors); Validator implementations store details about the errors in the Spring Errors object Spring makes the Errors object available to views for displaying error information to the user 27 Copyright 2005 Chariot Solutions

Sample Form Backing Object public class Beer { private long id; private String name; private Country country; private Money price; } 28 Copyright 2005 Chariot Solutions

Copyright 2005 Chariot Solutions Sample Application (Part 2)

New Spring MVC Features in 2.0 Expanded tag library Form tags (<spring:form>, <spring:input>, <spring:select>, <spring:textarea>, etc.) UrlFilenameViewController Transforms the virtual filename at the end of a URL into a view name and returns that view Can optionally prepend a prefix and/or append a suffix to build the view name /index.htm would return a view name of index ControllerClassNameHandlerMapping WelcomeController gets mapped to /welcome.htm HomeController gets mapped to /home.htm 30 Copyright 2005 Chariot Solutions

Thank You! mswartley@chariotsolutions.com Copyright 2005 Chariot Solutions

Spring Portlet MVC Build Portals the Spring Way Copyright 2005 Chariot Solutions

Agenda Overview of JSR 168 (Portlets) Overview of Spring Portlet MVC Basic request lifecycle DispatcherPortlet Handler Mapping Controllers View Resolvers Form processing 33 Copyright 2005 Chariot Solutions

Terminology A portal is a web based application that provides personalization, single sign on and content aggregation A portlet is a Java technology based web component, managed by a portlet container, which processes requests and generates dynamic content. 34 Copyright 2005 Chariot Solutions

Overview of JSR 168 JSR 168 is a specification from the Java Community Process for the standardization of portlets. The specification was developed to provide interoperability for running portlets on any vendor's implementation of the JSR 168 portlet container. The specification was approved in October of 2003. For more information about the Java Portlet Specification, see the JSR 168 specification: http://developers.sun.com/ prodtech/portalserver/reference/techart/jsr168/ 35 Copyright 2005 Chariot Solutions

Overview of JSR 168 (cont ) Very similar to the Servlet API Rendering is asynchronous Session attributes are scoped (Portlet or Application) Provides persistent storage for user preferences Portlets have Modes and Window States Request life cycle is different from Servlets 36 Copyright 2005 Chariot Solutions

Portlet Request Life Cycle Two types of requests action and render Action request asking portlet to perform some operation Render request asking portlet to display its content based on the current state API: processaction (ActionRequest, ActionResponse) render (RenderRequest, RenderResponse) 37 Copyright 2005 Chariot Solutions

Spring Portlet MVC Complete MVC framework for JSR 168 Portlets. Preserves JSR 168 unique lifecycle, i.e. RenderRequest and ActionRequest separation Most of the API is same or very close to Spring Servlet MVC Same benefits as Spring Servlet MVC Simplifies testing Simplifies binding form/request data to objects Simplifies handling of multiple view technologies And it is just fun to work with 38 Copyright 2005 Chariot Solutions

Copyright 2005 Chariot Solutions Dispatcher Portlet

DispatcherPortlet Front Controller for the Portlet Application Configured in portlet.xml <portlet id="beerportlet"> <portlet-name>beer</portlet-name> <portlet-class> org.springframework.web.portlet.dispatcherportlet </portlet-class> <supports> <mime-type>text/html</mime-type> <portlet-mode>view</portlet-mode> <portlet-mode>help</portlet-mode> </supports> <portlet-info> <title>beer Portlet</title> </portlet-info> </portlet> 40 Copyright 2005 Chariot Solutions

DispatcherPortlet (cont ) Loads configuration from <portlet-name>portlet.xml file (configurable with an initparam namespace ). <portlet-name>-portlet.xml contains configuration of the controllers and handler mappings for a given portlet Each portlet in the portlet web application has a unique namespace no co-mingling of beans. 41 Copyright 2005 Chariot Solutions

Handler Mapping A way to map each request to a controller. Can t use URLs for mapping Portal server is in charge of the URL generation. ParameterHandlerMapping Maps based on the request attribute name ( action by default) PortletModeHandlerMapping Maps based on the portlet mode name ( view, edit, help, etc) PortletModeParameterHandlerMapping Maps based on the combination of the two scenarios, first by mode and second action 42 Copyright 2005 Chariot Solutions

PortletModeHandlerMapping <!-- Fallback mapper - only defines controllers based on the mode --> <bean id="portletmodehandlermapping" class="org.s...web.portlet.handler.portletmodehandlermapping"> name="order" value="20"/> <property name="portletmodemap"> <map> <entry key="view"> <ref bean="welcomecontroller"/> </entry> <entry key="help"> <ref bean="helpcontroller"/> </entry> </map> </property> </bean> <property 43 Copyright 2005 Chariot Solutions

PortletModeParameterHandlerMappin <!-- In case of an ActionRequest pass request attribute to the render API --> <bean id="parametermappinginterceptor" class="org.springframework.web.portlet.handler.parametermappinginterceptor"/> <!-- mapping based on the mode and the request parameteter --> <bean id="portletmodeparameterhandlermapping class="org.s.web.portlet.handler.portletmodeparameterhandlermapping"> <property name="order" value="10"/> <property name="interceptors"> <list><ref bean="parametermappinginterceptor"/></list> </property> <property name="portletmodeparametermap"> <map><entry key="view"><map> <entry key="managebeers"><ref bean="managebeerscontroller"/></entry> <entry key="editbeer" value-ref="beerformcontroller" /> </map></entry></map> </property> </bean> 44 Copyright 2005 Chariot Solutions

Controllers Most controllers are similar to the Servlet MVC counterpart Controller (interface) BaseCommandController AbstractCommandController AbstractFormController SimpleFormController AbstractWizardFormController And new one (similar to ParameterizableViewController) PortletModeNameViewController determines view name based on the current portlet mode ( view, edit, help, or some custom mode) 45 Copyright 2005 Chariot Solutions

Controllers (continue) Controllers have two request handling APIs from Controller interfaces void handleactionrequest (ActionRequest, ActionResponse) ModelAndView handlerenderrequest (RenderRequest, RenderResponse) State of complex objects is shared with the PortletSession Use ActionResponse.setRenderParameter for simple String or String arrays 46 Copyright 2005 Chariot Solutions

SimpleFormController Same workflow as Servlet counterpart. Follow onsubmitaction or dosubmitaction chain for db updates. onsubmitrender methods may be executed repeatedly by the portal 47 Copyright 2005 Chariot Solutions

Controller Configuration Example There is no change in the way controllers are configured! Replace Servlet specific controllers with Portlet counterpart Modify controller code to account for Portlet API 48 Copyright 2005 Chariot Solutions

View Portlet MVC reuses the View contracts from Servlet MVC ViewRendererServlet is used to convert portlet request to servlet request <servlet> <servlet-name>viewrendererservlet</servlet-name> <servlet-class> o.s.web.servlet.viewrendererservlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>viewrendererservlet</servlet-name> <! URL is important. Modified with viewrendererurl on DispatcherPortlet <url-pattern>/web-inf/servlet/view</url-pattern> </servlet-mapping> 49 Copyright 2005 Chariot Solutions

Copyright 2005 Chariot Solutions Demo

Copyright 2005 Chariot Solutions Q&A