SlideShare a Scribd company logo
1 of 18
Download to read offline
Creating Software that Saves Lives
         Intro to Spring MVC
About Spring MVC

• Comes with the Spring distribution
• Well integrated with the rest of Spring
• Very extensible
Quick Spring Refresher

       • Write dependencies as setters
       • Link up dependencies in XML file
       • The Spring Framework will instantiate and
         call setters to hook it all together

<bean id=“dateFormat" class=“java.text.SimpleDateFormat">
  <constructor-arg value=“dd MMM yyyy”/>
</bean>

<bean id=“myBean" class=“com.platinumSolutions.stuff.SomeClass">
  <property name=“dateFormat” ref=“dateFormat”/>
</bean>
Spring MVC Basics

• All calls go through the DispatcherServlet
• Config file is *-servlet.xml by default

• MVC: instances of the following:
  – M – Model: a Java Map
  – V – View: org.springframework.web.servlet.View
  – C – Controller:
    org.springframework.web.servlet.mvc.Controller
Spring MVC Configuration

The configurable pieces of Spring MVC:

• org.springframework.web.servlet.HandlerMapping
   – what controller to call given a URL
• org.springframework.web.servlet.ViewResolver
   – how to determine what view to show
• org.springframework.web.servlet.LocaleResolver
   – how to determine internationalization
• org.springframework.web.multipart.MultipartResolver
   – how to handle uploaded files
• org.springframework.web.servlet.HandlerExceptionResolver
   – what to do with an Exception
• org.springframework.web.servlet.ThemeResolver
   – where to get css, images, pages from
• org.springframework.web.servlet.HandlerAdapter
   – wrapper around the controller (or servlet)
Spring MVC

A (simplified) sequence diagram:
Handling the Request with a
HandlerMapping
• Given a URL, figures out what Controller
  to use:

• SimpleUrlHandlerMapping
  – define mappings with Map or Properties
• BeanNameUrlHandlerMapping
  – bean names have same names as URL
• CommonsPathMapHandlerMapping
  – use Commons Attributes to determine
    mapping
Selecting a View with ViewResolver

• Given a view name, figures out what View
  to use:

•   BeanNameViewResolver
    – Spring beans happen to have the same name
•   UrlBasedViewResolver
    – view name maps to a URL (like a filename)
•   ResourceBundleViewResolver
    – look up the View in a resource file
•   XmlViewResolver
    – uses XML file to determine mappings
•   FreeMarkerViewResolver
    – UrlResourceViewResolver preset for FreeMarkerView
•   InternalResourceViewResolver
    – UrlResourceViewResolver preset for InternalResourceView
•   VelocityViewResolver
    – UrlResourceViewResolver preset for VelocityView
Different Views

• Plenty of Views are packaged with Spring
  MVC:

• JstlView
   – map to a JSP page
• RedirectView
   – Perform an HTTP Redirect
• TilesView, TilesJstlView
   – integration with tiles
• VelocityLayoutView, VelocityToolboxView, VelocityView
   – Integration with the Velocity templating tool
• FreeMarkerView
   – use the FreeMarker templating tool
• JasperReportsView, JasperReportsMultiFormatView,
  JasperReportsMultiFormatView,
  JasperReportsPdfView, JasperReportsXlsView
   – Support for Jasper Reports
Localization
• The locale may be chosen manually, selected by the
  browser, or fixed
   – AcceptHeaderLocaleResolver - use the HTTP accept-
     header to determine the locale
   – CookieLocalResolver - set the chosen locale in a cookie
   – FixedLocaleResolver - always use a fixed locale (set in the
     config file)
   – SessionLocaleResolver - store the chosen locale in the
     session
• The spring tag <spring:message> picks the resource
• Define the bean messageSource with a MessageSource to
  set the resources:
   – StaticMessageSource - set messages within the object
   – ResourceMessageBundleMessageSource - load messages
     from .properties files
   – ReloadableResourceMessageBundleMessageSource -
     same as above, but reloads!
   – (others)
Other Provided Controllers
• Spring MVC includes lots of Controllers to extend from:

• AbstractController
   – basic controller, knows about caching, turning on/off
     get/set/post/head
• ParameterizableViewController
   – always go to the same view
• UrlFileNameViewController
   – parses the URL to return a view (http://blah/foo.html -> foo)
• SimpleFormController
   – for form handling, hooks for attaching commands, validator
• AbstractWizardFormController
   – easy wizard controller
• ServletWrappingController
   – delegates to a servlet
Handling Forms

• Set the Command (just a bean)
• Set a Validator if needed (extend
  org.springframework.validation.Validator)
• Set destination views (form, success,
  failure, error)
• By default, uses GET/POST to determine
  whether it needs to load the form or
  process it
Wizards

• Similar to Forms, but needs to validate
  along the way
• One Controller handles multiple pages
• Process at the end
• Cancel anywhere along the line
• Spring Webflow is the now preferred
Interceptor

• Some HandlerMappings allow you to call
  an interceptor before the controller
• Useful for checking for session timeout,
  adding things to the request/session
• Kind of like AOP, but for Controllers
ExceptionHandler

• Spring philosophy says that most
  Exceptions should not be caught
• ExceptionHandler determines what to do if
  an Exception is thrown through the
  Controller
Themes

• Totally change look and feel of your
  application in one step!
• Lets you point to different css, jsp, images
Conclusion

• Spring MVC offers
  – Lots of flexibility
  – Straightforward design
  – Leverages Spring injection
Resources

• Websites
  – www.springframework.org
• Books
  – Professional Java Development with
    the Spring Framework, Johnson, et al
  – Pro Spring, Harrop & Machacek
  – Spring: A Developer’s Notebook,
    Tate
  – Spring in Action, Walls
  – Expert Spring MVC and Web Flow,
    Ladd

More Related Content

What's hot

What's hot (19)

Spring MVC
Spring MVCSpring MVC
Spring MVC
 
Spring Portlet MVC
Spring Portlet MVCSpring Portlet MVC
Spring Portlet MVC
 
Spring MVC Architecture Tutorial
Spring MVC Architecture TutorialSpring MVC Architecture Tutorial
Spring MVC Architecture Tutorial
 
Java Server Faces (JSF) - Basics
Java Server Faces (JSF) - BasicsJava Server Faces (JSF) - Basics
Java Server Faces (JSF) - Basics
 
Spring Web MVC
Spring Web MVCSpring Web MVC
Spring Web MVC
 
Integration of Backbone.js with Spring 3.1
Integration of Backbone.js with Spring 3.1Integration of Backbone.js with Spring 3.1
Integration of Backbone.js with Spring 3.1
 
Spring MVC 3.0 Framework (sesson_2)
Spring MVC 3.0 Framework (sesson_2)Spring MVC 3.0 Framework (sesson_2)
Spring MVC 3.0 Framework (sesson_2)
 
Introduction to Spring MVC
Introduction to Spring MVCIntroduction to Spring MVC
Introduction to Spring MVC
 
Servlets lecture1
Servlets lecture1Servlets lecture1
Servlets lecture1
 
Spring MVC Annotations
Spring MVC AnnotationsSpring MVC Annotations
Spring MVC Annotations
 
Implicit object.pptx
Implicit object.pptxImplicit object.pptx
Implicit object.pptx
 
JSP - Java Server Page
JSP - Java Server PageJSP - Java Server Page
JSP - Java Server Page
 
Spring Framework - MVC
Spring Framework - MVCSpring Framework - MVC
Spring Framework - MVC
 
MVC on the server and on the client
MVC on the server and on the clientMVC on the server and on the client
MVC on the server and on the client
 
Spring mvc 2.0
Spring mvc 2.0Spring mvc 2.0
Spring mvc 2.0
 
SpringMVC
SpringMVCSpringMVC
SpringMVC
 
Implicit objects advance Java
Implicit objects advance JavaImplicit objects advance Java
Implicit objects advance Java
 
Rest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.jsRest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.js
 
J2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsJ2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - Components
 

Viewers also liked

Spring MVC introduction HVA
Spring MVC introduction HVASpring MVC introduction HVA
Spring MVC introduction HVAPeter Maas
 
Spring 3 MVC CodeMash 2009
Spring 3 MVC   CodeMash 2009Spring 3 MVC   CodeMash 2009
Spring 3 MVC CodeMash 2009kensipe
 
Developing modular, polyglot applications with Spring (SpringOne India 2012)
Developing modular, polyglot applications with Spring (SpringOne India 2012)Developing modular, polyglot applications with Spring (SpringOne India 2012)
Developing modular, polyglot applications with Spring (SpringOne India 2012)Chris Richardson
 
What We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit TestingWhat We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit TestingKevlin Henney
 
Unidad 1 MOOC MVC
Unidad 1 MOOC MVCUnidad 1 MOOC MVC
Unidad 1 MOOC MVCscolomina
 
Spoken english classes in kalewadi phata
Spoken english classes in kalewadi phataSpoken english classes in kalewadi phata
Spoken english classes in kalewadi phataRaj Kumar
 
Become a Great Communicator
Become a Great CommunicatorBecome a Great Communicator
Become a Great CommunicatorTony Santiago
 
Speak better English here's how.
Speak better English here's how.Speak better English here's how.
Speak better English here's how.James Russell
 
1st qtr 16 use variety of sentences
1st qtr 16 use variety of sentences1st qtr 16 use variety of sentences
1st qtr 16 use variety of sentencesShirley Sison
 
Writing code you won't hate tomorrow
Writing code you won't hate tomorrowWriting code you won't hate tomorrow
Writing code you won't hate tomorrowRafael Dohms
 
Programming with Threads in Java
Programming with Threads in JavaProgramming with Threads in Java
Programming with Threads in Javakoji lin
 
Learn english | Talk English
Learn english | Talk EnglishLearn english | Talk English
Learn english | Talk EnglishGowri S
 
Learning spoken Language.
Learning spoken Language.Learning spoken Language.
Learning spoken Language.RoberAgainst
 

Viewers also liked (19)

Spring MVC introduction HVA
Spring MVC introduction HVASpring MVC introduction HVA
Spring MVC introduction HVA
 
Spring 3 MVC CodeMash 2009
Spring 3 MVC   CodeMash 2009Spring 3 MVC   CodeMash 2009
Spring 3 MVC CodeMash 2009
 
Developing modular, polyglot applications with Spring (SpringOne India 2012)
Developing modular, polyglot applications with Spring (SpringOne India 2012)Developing modular, polyglot applications with Spring (SpringOne India 2012)
Developing modular, polyglot applications with Spring (SpringOne India 2012)
 
What We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit TestingWhat We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit Testing
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
Unidad 1 MOOC MVC
Unidad 1 MOOC MVCUnidad 1 MOOC MVC
Unidad 1 MOOC MVC
 
Spoken english classes in kalewadi phata
Spoken english classes in kalewadi phataSpoken english classes in kalewadi phata
Spoken english classes in kalewadi phata
 
Become a Great Communicator
Become a Great CommunicatorBecome a Great Communicator
Become a Great Communicator
 
Speak better English here's how.
Speak better English here's how.Speak better English here's how.
Speak better English here's how.
 
1st qtr 16 use variety of sentences
1st qtr 16 use variety of sentences1st qtr 16 use variety of sentences
1st qtr 16 use variety of sentences
 
Writing code you won't hate tomorrow
Writing code you won't hate tomorrowWriting code you won't hate tomorrow
Writing code you won't hate tomorrow
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
 
Programming with Threads in Java
Programming with Threads in JavaProgramming with Threads in Java
Programming with Threads in Java
 
Learn english | Talk English
Learn english | Talk EnglishLearn english | Talk English
Learn english | Talk English
 
Basic java tutorial
Basic java tutorialBasic java tutorial
Basic java tutorial
 
Mis ethical social
Mis ethical socialMis ethical social
Mis ethical social
 
Threads in java
Threads in javaThreads in java
Threads in java
 
Memory Module Network - MMN
Memory Module Network - MMNMemory Module Network - MMN
Memory Module Network - MMN
 
Learning spoken Language.
Learning spoken Language.Learning spoken Language.
Learning spoken Language.
 

Similar to Spring mvc

Spring 3.1 to 3.2 in a Nutshell - Spring I/O 2012
Spring 3.1 to 3.2 in a Nutshell - Spring I/O 2012Spring 3.1 to 3.2 in a Nutshell - Spring I/O 2012
Spring 3.1 to 3.2 in a Nutshell - Spring I/O 2012Sam Brannen
 
Spring 3.1 in a Nutshell - JAX London 2011
Spring 3.1 in a Nutshell - JAX London 2011Spring 3.1 in a Nutshell - JAX London 2011
Spring 3.1 in a Nutshell - JAX London 2011Sam Brannen
 
Spring Day | Spring 3.1 in a Nutshell | Sam Brannen
Spring Day | Spring 3.1 in a Nutshell | Sam BrannenSpring Day | Spring 3.1 in a Nutshell | Sam Brannen
Spring Day | Spring 3.1 in a Nutshell | Sam BrannenJAX London
 
Spring 3.1 in a Nutshell
Spring 3.1 in a NutshellSpring 3.1 in a Nutshell
Spring 3.1 in a NutshellSam Brannen
 
Apache Cayenne for WO Devs
Apache Cayenne for WO DevsApache Cayenne for WO Devs
Apache Cayenne for WO DevsWO Community
 
Spring 3.1: a Walking Tour
Spring 3.1: a Walking TourSpring 3.1: a Walking Tour
Spring 3.1: a Walking TourJoshua Long
 
Elements for an iOS Backend
Elements for an iOS BackendElements for an iOS Backend
Elements for an iOS BackendLaurent Cerveau
 
Toms introtospring mvc
Toms introtospring mvcToms introtospring mvc
Toms introtospring mvcGuo Albert
 
PLAT-8 Spring Web Scripts and Spring Surf
PLAT-8 Spring Web Scripts and Spring SurfPLAT-8 Spring Web Scripts and Spring Surf
PLAT-8 Spring Web Scripts and Spring SurfAlfresco Software
 
Effective out-of-container Integration Testing
Effective out-of-container Integration TestingEffective out-of-container Integration Testing
Effective out-of-container Integration TestingSam Brannen
 
Spring Framework 4.1
Spring Framework 4.1Spring Framework 4.1
Spring Framework 4.1Sam Brannen
 
Introduction to ASP.Net MVC
Introduction to ASP.Net MVCIntroduction to ASP.Net MVC
Introduction to ASP.Net MVCSagar Kamate
 
ASP.NET - Building Web Application..in the right way!
ASP.NET - Building Web Application..in the right way!ASP.NET - Building Web Application..in the right way!
ASP.NET - Building Web Application..in the right way!Fioriela Bego
 
ASP.NET - Building Web Application..in the right way!
ASP.NET - Building Web Application..in the right way!ASP.NET - Building Web Application..in the right way!
ASP.NET - Building Web Application..in the right way!Commit Software Sh.p.k.
 
Desired state-configuration-ravikanth-august-2013-vtc india
Desired state-configuration-ravikanth-august-2013-vtc indiaDesired state-configuration-ravikanth-august-2013-vtc india
Desired state-configuration-ravikanth-august-2013-vtc indiaRavikanth Chaganti
 
Cloud computing 3702
Cloud computing 3702Cloud computing 3702
Cloud computing 3702Jess Coburn
 
Backbonification for dummies - Arrrrug 10/1/2012
Backbonification for dummies - Arrrrug 10/1/2012Backbonification for dummies - Arrrrug 10/1/2012
Backbonification for dummies - Arrrrug 10/1/2012Dimitri de Putte
 
Java EE 8 Web Frameworks: A Look at JSF vs MVC
Java EE 8 Web Frameworks: A Look at JSF vs MVCJava EE 8 Web Frameworks: A Look at JSF vs MVC
Java EE 8 Web Frameworks: A Look at JSF vs MVCJosh Juneau
 
Testing Spring MVC and REST Web Applications
Testing Spring MVC and REST Web ApplicationsTesting Spring MVC and REST Web Applications
Testing Spring MVC and REST Web ApplicationsSam Brannen
 
Asp 1-mvc introduction
Asp 1-mvc introductionAsp 1-mvc introduction
Asp 1-mvc introductionFajar Baskoro
 

Similar to Spring mvc (20)

Spring 3.1 to 3.2 in a Nutshell - Spring I/O 2012
Spring 3.1 to 3.2 in a Nutshell - Spring I/O 2012Spring 3.1 to 3.2 in a Nutshell - Spring I/O 2012
Spring 3.1 to 3.2 in a Nutshell - Spring I/O 2012
 
Spring 3.1 in a Nutshell - JAX London 2011
Spring 3.1 in a Nutshell - JAX London 2011Spring 3.1 in a Nutshell - JAX London 2011
Spring 3.1 in a Nutshell - JAX London 2011
 
Spring Day | Spring 3.1 in a Nutshell | Sam Brannen
Spring Day | Spring 3.1 in a Nutshell | Sam BrannenSpring Day | Spring 3.1 in a Nutshell | Sam Brannen
Spring Day | Spring 3.1 in a Nutshell | Sam Brannen
 
Spring 3.1 in a Nutshell
Spring 3.1 in a NutshellSpring 3.1 in a Nutshell
Spring 3.1 in a Nutshell
 
Apache Cayenne for WO Devs
Apache Cayenne for WO DevsApache Cayenne for WO Devs
Apache Cayenne for WO Devs
 
Spring 3.1: a Walking Tour
Spring 3.1: a Walking TourSpring 3.1: a Walking Tour
Spring 3.1: a Walking Tour
 
Elements for an iOS Backend
Elements for an iOS BackendElements for an iOS Backend
Elements for an iOS Backend
 
Toms introtospring mvc
Toms introtospring mvcToms introtospring mvc
Toms introtospring mvc
 
PLAT-8 Spring Web Scripts and Spring Surf
PLAT-8 Spring Web Scripts and Spring SurfPLAT-8 Spring Web Scripts and Spring Surf
PLAT-8 Spring Web Scripts and Spring Surf
 
Effective out-of-container Integration Testing
Effective out-of-container Integration TestingEffective out-of-container Integration Testing
Effective out-of-container Integration Testing
 
Spring Framework 4.1
Spring Framework 4.1Spring Framework 4.1
Spring Framework 4.1
 
Introduction to ASP.Net MVC
Introduction to ASP.Net MVCIntroduction to ASP.Net MVC
Introduction to ASP.Net MVC
 
ASP.NET - Building Web Application..in the right way!
ASP.NET - Building Web Application..in the right way!ASP.NET - Building Web Application..in the right way!
ASP.NET - Building Web Application..in the right way!
 
ASP.NET - Building Web Application..in the right way!
ASP.NET - Building Web Application..in the right way!ASP.NET - Building Web Application..in the right way!
ASP.NET - Building Web Application..in the right way!
 
Desired state-configuration-ravikanth-august-2013-vtc india
Desired state-configuration-ravikanth-august-2013-vtc indiaDesired state-configuration-ravikanth-august-2013-vtc india
Desired state-configuration-ravikanth-august-2013-vtc india
 
Cloud computing 3702
Cloud computing 3702Cloud computing 3702
Cloud computing 3702
 
Backbonification for dummies - Arrrrug 10/1/2012
Backbonification for dummies - Arrrrug 10/1/2012Backbonification for dummies - Arrrrug 10/1/2012
Backbonification for dummies - Arrrrug 10/1/2012
 
Java EE 8 Web Frameworks: A Look at JSF vs MVC
Java EE 8 Web Frameworks: A Look at JSF vs MVCJava EE 8 Web Frameworks: A Look at JSF vs MVC
Java EE 8 Web Frameworks: A Look at JSF vs MVC
 
Testing Spring MVC and REST Web Applications
Testing Spring MVC and REST Web ApplicationsTesting Spring MVC and REST Web Applications
Testing Spring MVC and REST Web Applications
 
Asp 1-mvc introduction
Asp 1-mvc introductionAsp 1-mvc introduction
Asp 1-mvc introduction
 

More from Guo Albert

AWS IAM (Identity and Access Management) Policy Simulator
AWS IAM (Identity and Access Management) Policy SimulatorAWS IAM (Identity and Access Management) Policy Simulator
AWS IAM (Identity and Access Management) Policy SimulatorGuo Albert
 
TOEIC 準備心得
TOEIC 準備心得TOEIC 準備心得
TOEIC 準備心得Guo Albert
 
DBM專案環境建置
DBM專案環境建置DBM專案環境建置
DBM專案環境建置Guo Albert
 
JPA Optimistic Locking With @Version
JPA Optimistic Locking With @VersionJPA Optimistic Locking With @Version
JPA Optimistic Locking With @VersionGuo Albert
 
OCEJPA Study Notes
OCEJPA Study NotesOCEJPA Study Notes
OCEJPA Study NotesGuo Albert
 
OCEJPA(1Z0-898) Preparation Tips
OCEJPA(1Z0-898) Preparation TipsOCEJPA(1Z0-898) Preparation Tips
OCEJPA(1Z0-898) Preparation TipsGuo Albert
 
JPA lifecycle events practice
JPA lifecycle events practiceJPA lifecycle events practice
JPA lifecycle events practiceGuo Albert
 
XDate - a modern java-script date library
XDate -  a modern java-script date libraryXDate -  a modern java-script date library
XDate - a modern java-script date libraryGuo Albert
 
How to avoid check style errors
How to avoid check style errorsHow to avoid check style errors
How to avoid check style errorsGuo Albert
 
NIG系統報表開發指南
NIG系統報表開發指南NIG系統報表開發指南
NIG系統報表開發指南Guo Albert
 
Ease Your Effort of Putting Data into History Table
Ease Your Effort of Putting Data into History TableEase Your Effort of Putting Data into History Table
Ease Your Effort of Putting Data into History TableGuo Albert
 
NIG 系統開發指引
NIG 系統開發指引NIG 系統開發指引
NIG 系統開發指引Guo Albert
 
NIG系統開發文件閱讀步驟
NIG系統開發文件閱讀步驟NIG系統開發文件閱讀步驟
NIG系統開發文件閱讀步驟Guo Albert
 
Form Bean Creation Process for NIG System
Form Bean Creation Process for NIG SystemForm Bean Creation Process for NIG System
Form Bean Creation Process for NIG SystemGuo Albert
 
A Short Intorduction to JasperReports
A Short Intorduction to JasperReportsA Short Intorduction to JasperReports
A Short Intorduction to JasperReportsGuo Albert
 
Apply Template Method Pattern in Report Implementation
Apply Template Method Pattern in Report ImplementationApply Template Method Pattern in Report Implementation
Apply Template Method Pattern in Report ImplementationGuo Albert
 
Utilize Commons BeansUtils to do copy object
Utilize Commons BeansUtils to do copy objectUtilize Commons BeansUtils to do copy object
Utilize Commons BeansUtils to do copy objectGuo Albert
 
Apply my eclipse to do entity class generation
Apply my eclipse to do entity class generationApply my eclipse to do entity class generation
Apply my eclipse to do entity class generationGuo Albert
 
Nig project setup quickly tutorial
Nig project setup quickly tutorialNig project setup quickly tutorial
Nig project setup quickly tutorialGuo Albert
 
Spring JDBCTemplate
Spring JDBCTemplateSpring JDBCTemplate
Spring JDBCTemplateGuo Albert
 

More from Guo Albert (20)

AWS IAM (Identity and Access Management) Policy Simulator
AWS IAM (Identity and Access Management) Policy SimulatorAWS IAM (Identity and Access Management) Policy Simulator
AWS IAM (Identity and Access Management) Policy Simulator
 
TOEIC 準備心得
TOEIC 準備心得TOEIC 準備心得
TOEIC 準備心得
 
DBM專案環境建置
DBM專案環境建置DBM專案環境建置
DBM專案環境建置
 
JPA Optimistic Locking With @Version
JPA Optimistic Locking With @VersionJPA Optimistic Locking With @Version
JPA Optimistic Locking With @Version
 
OCEJPA Study Notes
OCEJPA Study NotesOCEJPA Study Notes
OCEJPA Study Notes
 
OCEJPA(1Z0-898) Preparation Tips
OCEJPA(1Z0-898) Preparation TipsOCEJPA(1Z0-898) Preparation Tips
OCEJPA(1Z0-898) Preparation Tips
 
JPA lifecycle events practice
JPA lifecycle events practiceJPA lifecycle events practice
JPA lifecycle events practice
 
XDate - a modern java-script date library
XDate -  a modern java-script date libraryXDate -  a modern java-script date library
XDate - a modern java-script date library
 
How to avoid check style errors
How to avoid check style errorsHow to avoid check style errors
How to avoid check style errors
 
NIG系統報表開發指南
NIG系統報表開發指南NIG系統報表開發指南
NIG系統報表開發指南
 
Ease Your Effort of Putting Data into History Table
Ease Your Effort of Putting Data into History TableEase Your Effort of Putting Data into History Table
Ease Your Effort of Putting Data into History Table
 
NIG 系統開發指引
NIG 系統開發指引NIG 系統開發指引
NIG 系統開發指引
 
NIG系統開發文件閱讀步驟
NIG系統開發文件閱讀步驟NIG系統開發文件閱讀步驟
NIG系統開發文件閱讀步驟
 
Form Bean Creation Process for NIG System
Form Bean Creation Process for NIG SystemForm Bean Creation Process for NIG System
Form Bean Creation Process for NIG System
 
A Short Intorduction to JasperReports
A Short Intorduction to JasperReportsA Short Intorduction to JasperReports
A Short Intorduction to JasperReports
 
Apply Template Method Pattern in Report Implementation
Apply Template Method Pattern in Report ImplementationApply Template Method Pattern in Report Implementation
Apply Template Method Pattern in Report Implementation
 
Utilize Commons BeansUtils to do copy object
Utilize Commons BeansUtils to do copy objectUtilize Commons BeansUtils to do copy object
Utilize Commons BeansUtils to do copy object
 
Apply my eclipse to do entity class generation
Apply my eclipse to do entity class generationApply my eclipse to do entity class generation
Apply my eclipse to do entity class generation
 
Nig project setup quickly tutorial
Nig project setup quickly tutorialNig project setup quickly tutorial
Nig project setup quickly tutorial
 
Spring JDBCTemplate
Spring JDBCTemplateSpring JDBCTemplate
Spring JDBCTemplate
 

Recently uploaded

AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 

Recently uploaded (20)

AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 

Spring mvc

  • 1. Creating Software that Saves Lives Intro to Spring MVC
  • 2. About Spring MVC • Comes with the Spring distribution • Well integrated with the rest of Spring • Very extensible
  • 3. Quick Spring Refresher • Write dependencies as setters • Link up dependencies in XML file • The Spring Framework will instantiate and call setters to hook it all together <bean id=“dateFormat" class=“java.text.SimpleDateFormat"> <constructor-arg value=“dd MMM yyyy”/> </bean> <bean id=“myBean" class=“com.platinumSolutions.stuff.SomeClass"> <property name=“dateFormat” ref=“dateFormat”/> </bean>
  • 4. Spring MVC Basics • All calls go through the DispatcherServlet • Config file is *-servlet.xml by default • MVC: instances of the following: – M – Model: a Java Map – V – View: org.springframework.web.servlet.View – C – Controller: org.springframework.web.servlet.mvc.Controller
  • 5. Spring MVC Configuration The configurable pieces of Spring MVC: • org.springframework.web.servlet.HandlerMapping – what controller to call given a URL • org.springframework.web.servlet.ViewResolver – how to determine what view to show • org.springframework.web.servlet.LocaleResolver – how to determine internationalization • org.springframework.web.multipart.MultipartResolver – how to handle uploaded files • org.springframework.web.servlet.HandlerExceptionResolver – what to do with an Exception • org.springframework.web.servlet.ThemeResolver – where to get css, images, pages from • org.springframework.web.servlet.HandlerAdapter – wrapper around the controller (or servlet)
  • 6. Spring MVC A (simplified) sequence diagram:
  • 7. Handling the Request with a HandlerMapping • Given a URL, figures out what Controller to use: • SimpleUrlHandlerMapping – define mappings with Map or Properties • BeanNameUrlHandlerMapping – bean names have same names as URL • CommonsPathMapHandlerMapping – use Commons Attributes to determine mapping
  • 8. Selecting a View with ViewResolver • Given a view name, figures out what View to use: • BeanNameViewResolver – Spring beans happen to have the same name • UrlBasedViewResolver – view name maps to a URL (like a filename) • ResourceBundleViewResolver – look up the View in a resource file • XmlViewResolver – uses XML file to determine mappings • FreeMarkerViewResolver – UrlResourceViewResolver preset for FreeMarkerView • InternalResourceViewResolver – UrlResourceViewResolver preset for InternalResourceView • VelocityViewResolver – UrlResourceViewResolver preset for VelocityView
  • 9. Different Views • Plenty of Views are packaged with Spring MVC: • JstlView – map to a JSP page • RedirectView – Perform an HTTP Redirect • TilesView, TilesJstlView – integration with tiles • VelocityLayoutView, VelocityToolboxView, VelocityView – Integration with the Velocity templating tool • FreeMarkerView – use the FreeMarker templating tool • JasperReportsView, JasperReportsMultiFormatView, JasperReportsMultiFormatView, JasperReportsPdfView, JasperReportsXlsView – Support for Jasper Reports
  • 10. Localization • The locale may be chosen manually, selected by the browser, or fixed – AcceptHeaderLocaleResolver - use the HTTP accept- header to determine the locale – CookieLocalResolver - set the chosen locale in a cookie – FixedLocaleResolver - always use a fixed locale (set in the config file) – SessionLocaleResolver - store the chosen locale in the session • The spring tag <spring:message> picks the resource • Define the bean messageSource with a MessageSource to set the resources: – StaticMessageSource - set messages within the object – ResourceMessageBundleMessageSource - load messages from .properties files – ReloadableResourceMessageBundleMessageSource - same as above, but reloads! – (others)
  • 11. Other Provided Controllers • Spring MVC includes lots of Controllers to extend from: • AbstractController – basic controller, knows about caching, turning on/off get/set/post/head • ParameterizableViewController – always go to the same view • UrlFileNameViewController – parses the URL to return a view (http://blah/foo.html -> foo) • SimpleFormController – for form handling, hooks for attaching commands, validator • AbstractWizardFormController – easy wizard controller • ServletWrappingController – delegates to a servlet
  • 12. Handling Forms • Set the Command (just a bean) • Set a Validator if needed (extend org.springframework.validation.Validator) • Set destination views (form, success, failure, error) • By default, uses GET/POST to determine whether it needs to load the form or process it
  • 13. Wizards • Similar to Forms, but needs to validate along the way • One Controller handles multiple pages • Process at the end • Cancel anywhere along the line • Spring Webflow is the now preferred
  • 14. Interceptor • Some HandlerMappings allow you to call an interceptor before the controller • Useful for checking for session timeout, adding things to the request/session • Kind of like AOP, but for Controllers
  • 15. ExceptionHandler • Spring philosophy says that most Exceptions should not be caught • ExceptionHandler determines what to do if an Exception is thrown through the Controller
  • 16. Themes • Totally change look and feel of your application in one step! • Lets you point to different css, jsp, images
  • 17. Conclusion • Spring MVC offers – Lots of flexibility – Straightforward design – Leverages Spring injection
  • 18. Resources • Websites – www.springframework.org • Books – Professional Java Development with the Spring Framework, Johnson, et al – Pro Spring, Harrop & Machacek – Spring: A Developer’s Notebook, Tate – Spring in Action, Walls – Expert Spring MVC and Web Flow, Ladd