Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Spring MVC Essentials Getting started.

Slides:



Advertisements
Similar presentations
3 Copyright © 2005, Oracle. All rights reserved. Designing J2EE Applications.
Advertisements

Spring 3.0 MVC - Introduction By, Srinivas Reddy.S
Web Development with Karsten Schulz Terp-Nielsen Master Principal Sales Consultant Oracle Denmark.
Apache Struts Technology
A Blackboard Building Block™ Crash Course for Web Developers
Struts Basics SSE USTC Qing Ding. Agenda What is and Why Struts? Struts architecture – Controller: Focus of this presentation – Model – View Struts tag.
Application Architectures Vijayan Sugumaran Department of DIS Oakland University.
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.
Web Applications Basics. Introduction to Web Web features Clent/Server HTTP HyperText Markup Language URL addresses Web server - a computer program that.
Intro to Spring CJUG - January What is Spring? “The Spring framework provides central transaction control of various objects.” This means that any.
Struts 2.0 an Overview ( )
Introduction to Struts 2.0 Jenny Ni Joey Feng Winddays Wang Hewmmi Zhu Heather Lv Software School,Fudan University 1.
Struts. Agenda Preface Struts and its components An example The architecture required for Struts Applications.
UNIT-V The MVC architecture and Struts Framework.
Lecture 2 - Struts ENTERPRISE JAVA. 2 Contents  Servlet Deployment  Servlet Filters  Model View Controllers  Struts  Dependency Injection.
Windows.Net Programming Series Preview. Course Schedule CourseDate Microsoft.Net Fundamentals 01/13/2014 Microsoft Windows/Web Fundamentals 01/20/2014.
Spring Overview, Application demo -Midhila Paineni 09/23/2011 Spring Overview, Application demo9/8/20151.
Copyright © 2012 Accenture All Rights Reserved.Copyright © 2012 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are.
Copyright © 2012 Accenture All Rights Reserved.Copyright © 2012 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are.
OpusCollege and the use of Spring and iBatis
Basic Struts Architecture Client Server Database Request Response Control View Model Server Struts Framework.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Understanding the Bean Lifecycle An.
8/15/2003System Specialists Corporation How to implement BC4J with the Struts Framework Eugene Sicat Technical Architect/Project Manager System Specialists.
JSF Introduction Copyright © Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in print without written permission.
Lecturer: Prof. Piero Fraternali, Teaching Assistant: Alessandro Bozzon, Advanced Web Technologies: Struts–
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
Fall CIS 764 Database Systems Design L8. Web ….
JSTL Lec Umair©2006, All rights reserved JSTL (ni) Acronym of  JavaServer Pages Standard Tag Library JSTL (like JSP) is a specification, not an.
Opus College – Techniques Spring Annotated Controllers.
Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer Architecture.
1 Kuali Nervous System (KNS) Part 2 Presented by: Jerry Neal – KFS Development Manager Geoff McGregor – KC Lead Developer Brian McGough – KRice Project.
A Presentation By V AIBHAV S AHARAN Web-enHanced Information Management COMS E6125.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Forms with Spring MVC Handling Form.
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.
Struts 2 introduction. Struts 2 framework Struts 2 A full-featured web application framework for the Java EE platform The Java Servlet API exposes the.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Spring MVC Views Rendering custom views.
Chính phủ điện tử TS. Phạm Văn Tính Khoa CNTT, ĐH Nông Lâm TP.HCM
How I spend my money Software architecture course Mohan, Maxim.
© 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.
1 Copyright © 2004, Oracle. All rights reserved. Oracle Application Development Framework.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Data Access with Spring.
Implementation Struts Framework for well-architectured web applications Model-View-Controller design pattern.
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
Chapter 9 Web Application Design. Objectives Describe the MVC design pattern as used with Web applications Explain the role and responsibilities of each.
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.
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.
By Jonathan Smith. Road Map Introduction Company Information Project Overview Java Web Design and Development Summary Relation to IUP Acknowledgments.
Apache Struts Technology A MVC Framework for Java Web Applications.
Model View Controller. MVC is the separation of model, view and controller. It's simply a paradigm; an ideal that you should have in the back of your.
Enterprise Java v050228MVC1 Model, View, Controller Web Architecture.
CS520 Web Programming Spring – Web MVC Chengyu Sun California State University, Los Angeles.
Java Servlets By: Tejashri Udavant..
J2EE Lecture 7: Spring – Spring MVC
Knowledge Byte In this section, you will learn about:
Unit 6-Chapter 2 Struts.
Design and Maintenance of Web Applications in J2EE
Apache Tuscany Demo BigBank Example
CS5220 Advanced Topics in Web Programming Spring – Web MVC
The Model Layer What is Model?
JavaServer Faces: The Fundamentals
Struts BY: Tejashri Udavant..
SDMX IT Tools SDMX Registry
Presentation transcript:

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Spring MVC Essentials Getting started with Spring’s web application development framework

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 2 Topics in this Session Overview of Spring MVC Spring MVC Request Processing Lifecycle Key Spring MVC Artifacts –DispatcherServlet –Handlers –Handler Mappings –Views Quick Start

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 3 Topics in this Session Overview of Spring MVC Spring MVC Request Processing Lifecycle Key Spring MVC Artifacts –DispatcherServlet –Handlers –Handler Mappings –Views Quick Start

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 4 Spring MVC Overview Spring’s web framework –Optional component built on top of the Spring framework Builds on the Java Servlet API –A parallel Portlet version is also provided Comparable to Struts –A request-driven action framework –Easy for Struts users to adopt while providing a stronger architectural foundation

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 5 Spring MVC Architectural Strengths Simple model –Easy to get going –Core architecture has few external dependencies Designed for extension –Smart extension points put you in control –Layered architecture facilitates reuse Strong integration –Integrates popular view and controller technologies Uses Spring itself for configuration –All artifacts are testable and benefit from dependency injection

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 6 Spring MVC 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 Other Key Features Data Binding Framework Annotation-based Configuration Validation Framework Internationalization Support Tag Library

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 8 Topics in this Session Overview of Spring MVC Spring MVC Request Processing Lifecycle Key Spring MVC Artifacts –DispatcherServlet –Handlers –Handler Mappings –Views Quick Start

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 9 High-Level Spring MVC Request Processing Lifecycle Dispatcher Servlet Dispatcher Servlet Handler View request dispatch request result view render

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 10 Request Processing Sequence Dispatcher Servlet Dispatcher Servlet HandlerMapping Handler View service (request, response) getHandler(request) handleRequest (request, response) result view render(result, response)

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 11 Topics in this Session Overview of Spring MVC Spring MVC Request Processing Lifecycle Key Spring MVC Artifacts –DispatcherServlet –Handlers –Handler Mappings –Views Quick Start

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 12 Dispatcher Servlet The Heart of Spring MVC A Dispatcher Servlet is a front controller that coordinates the processing of all requests into the web application –Dispatches requests to handlers –Issues appropriate responses to clients Analogous to a Struts Action Servlet –But more powerful and customizable Deployable as a standard Java Servlet

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 13 Dispatcher Servlet Configuration Uses Spring for configuration –Has its own Spring ApplicationContext managing web-layer beans you define –Delegates to these beans to carry out request processing Has full access to the “root” application context –Enables web beans to obtain references to application beans through dependency injection

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 14 Dispatcher Servlet Usage Considerations DispatcherServlets are coarse-grained –Define one per logical web application –Not one per use case Generic dispatcher infrastructure can be used for several responsibilities –To dispatch requests for HTML content –To dispatch requests to web services

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 15 handleRequest (request, response) Request Handlers Incoming requests are dispatched to handlers –There are potentially many handlers per DispatcherServlet The Dispatcher’s HandlerMapping determines the handler of each request Dispatcher Servlet Dispatcher Servlet HandlerMapping Handler getHandler(request)

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 16 Most HandlerMapping Rules Are Based On URLs URL patterns can be configured in Java public void login() Or in XML –/login.htm=loginHandler –/editAccount.htm=editAccountHandler –/reward/*/**=rewardHandler

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 17 Two Different Styles of Handlers Handlers are annotated Or, implement the Controller public class MyLoginHandler { ModelAndView login(HttpServletRequest request) { } public class MyLoginHandler { ModelAndView login(HttpServletRequest request) { } } public interface Controller { ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception; } public interface Controller { ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception; }

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 18 Spring MVC Renders a ModelAndView To render a response to the client, Spring MVC needs a ModelAndView –Selects the view to render the response –Contains the data needed for rendering The Model is the contract between the Controller and the View The same Controller often returns different ModelAndView objects –To render different types of responses for different types of actions

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 19 ModelAndView Result Object ModelAndView View Model (a java.util.Map) Model (a java.util.Map) Controller Controller returns

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 20 Example Controllers An AccountController that creates, shows, updates, and deletes Accounts in the database A LoginController that logs users in A TopPerformingAccountsController that generates an Excel spreadsheet showing the top 20 accounts

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 21 Views A Controller returns a view to render the model –The View generates the HTTP response A Controller may return a concrete View implementation like –new InternalResourceView(“/WEB-INF/reward/list.jsp”) –new RewardListingPdf() Or a view name like “list” –The DispatcherServlet will resolve the name to a View implementation Or null –The DispatcherServlet will select the view for the request

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 22 View Resolvers The DispatcherServlet delegates to a ViewResolver to map returned view names to View implementations The default ViewResolver treats the view name as a Web Application-relative file path Override this default by registering a ViewResolver bean with the DispatcherServlet Common view resolver strategies –Internal resource –Bean name

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 23 Custom Internal Resource View Resolver Example RewardController handle GET /reward/list rewardList model={rewards} DispatcherServlet Custom InternalResource ViewResolver Custom InternalResource ViewResolver rewardList /WEB-INF/views/rewardList.jsp 12 Logical view name Resolved physical path

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 24 Topics in this Session Overview of Spring MVC Spring MVC Request Processing Lifecycle Key Spring MVC Artifacts –DispatcherServlet –Handlers –Handler Mappings –Views Quick Start

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 25 Quick Start Steps to developing web application functionality with Spring MVC 1.Deploy a Dispatcher Servlet (one-time only) 2.Implement a Controller 3.Implement the View(s) 4.Register the Controller with the DispatcherServlet 5.Deploy and test Repeat steps 2-5 to develop new functionality

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited Deploy a DispatcherServlet Define inside within web.xml rewardsadmin org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/rewardsadmin-servlet-config.xml rewardsadmin org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/rewardsadmin-servlet-config.xml Contains the Servlet’s configuration

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited Deploy a Dispatcher Servlet (2) Map the Servlet to a URL pattern Will now be able to invoke the Servlet like rewardsadmin /* rewardsadmin /*

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 28 Initial Dispatcher Servlet Config <!-- Define your “web beans” here such as your Controllers and their URL mapping configurations Web beans here may reference “application beans” in the “root” web application context --> <!-- Define your “web beans” here such as your Controllers and their URL mapping configurations Web beans here may reference “application beans” in the “root” web application context --> /WEB-INF/rewardsadmin-servlet-config.xml

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 29 After DispatcherServlet Deployment Root WebApplicationContext All web beans will have access to application beans Java EE™ Web Application DispatcherServlet ApplicationContext

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited Implement a public class RewardController { private RewardLookupService lookupService; RewardController(RewardLookupService rls) { this.lookupService = rls; public ModelAndView showReward(HttpServletRequest request) { String id = request.getParameter(“id”); Reward reward = lookupService.lookupReward(id); ModelAndView result = new ModelAndView(); result.addObject(“reward”, reward); result.setViewName(“rewardView”); return result; public class RewardController { private RewardLookupService lookupService; RewardController(RewardLookupService rls) { this.lookupService = rls; public ModelAndView showReward(HttpServletRequest request) { String id = request.getParameter(“id”); Reward reward = lookupService.lookupReward(id); ModelAndView result = new ModelAndView(); result.addObject(“reward”, reward); result.setViewName(“rewardView”); return result; } } Selects the “rewardView” to render the “reward” model Depends on application service

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited Implement the Views Your Reward Amount=${reward.amount} Date=${reward.date} Account Number=${reward.account} Merchant Number=${reward.merchant} Your Reward Amount=${reward.amount} Date=${reward.date} Account Number=${reward.account} Merchant Number=${reward.merchant} /WEB-INF/views/rewardView.jsp References result model object by name

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited Register the Controller with the DispatcherServlet /WEB-INF/rewardsadmin-servlet-config.xml

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited Deploy and Test Your Reward Amount = $ Date = 2006/12/29 Account Number = Merchant Number = Your Reward Amount = $ Date = 2006/12/29 Account Number = Merchant Number =

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 34 Summary of Request Lifecycle Dispatcher Servlet Dispatcher Servlet HandlerMapping Reward Controller Reward Controller JstlView rewardView.jsp JstlView rewardView.jsp getHandler(request) showReward (request) rewardView model={reward} render(model, response) /reward/show?id=1

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. LAB Spring MVC Essentials