SlideShare a Scribd company logo
Spring MVC
By Deepak Bhagat
Topics
 Mapping request to Spring controllers
 Transparently binding form parameters.
 Validating form submissions.
 Uploading files.
Request in MVC
1. DispatcherServlet is the first stop
for request,
2. DS act as Front controller which
delegates responsibility for a
request to other component to
perform actual processing.
3. DS job is to send request to
Controller which DS does by the
help of Handler mapping
4. At the controller the request drop off the payload(content send by user) and
patiently wait while the controller process the information*
5. The controller pack the processed data in Model* and identifies the view to render.
It then send the request along with model and view to DispatcherServlet.
6. The DS will consult the view resolver to map the logical view name to specified
view implementation.
Setting up Spring MVC
1. DispatcherServlet must be configured in web.xml
2. Because the servlet name is spitter DS will try to load spitter-servlet.xml
3. Next we must indicate what url will be handled by DS
4. We must also think of how the static resource should be handled, spring has new
element for this purpose, Below content goes to spitter-servlet.xml
5. All our images, stylesheet, javascript need to be kept in resources folder.
Writing a basic controller
1. Before we start with writing controller. Lets understand bit about HandlerMapping
DispatcherServlet consult to find controller.
2. BeanNameUrlHandlerMapping, ConrollerBeanNameUrlMapping,
ConrollerClassNameHanderMapping, DefaultAnnotationHandlerMapping,
SimpleUrlHandlerMapping.
3. Using handler mapping is just about configuration. If no handler mapping is found
then DispatcherServlet uses BeanNameUrlHandlerMapping and
DefaultAnnotationHandlerMapping.
4. DefaultAnnotationHandlerMapping maps request to controller methods that are
annotated with @RequestMapping .
5. Annotations make most of the task simple we will use it to bind request parameter
to handle method parameters, perform validation and perform message conversion
and we just have to configure a single line in splitter-servlet.xml.
1. ````````
Writing a basic controller
1. @controller indicate this is a controller class. @controller is specialization of
@component which means that <context:component-scan> will pick up and register it
as bean. Which mean <context:componsent-scan> need to be configured in spitter-
servlet.xml.
2. @Inject : Automatically inject bean when controller is instantiated
Writing a basic controller
3. @RequestMapping : It serves two purpose. First, it identifies showHomePage() as
request handling method for url pattern (/ or /home). Second, to render data to view*
Model is populated with key value pair. It then return logical name.
View resolver job is to map a logical view name to some implementation of
org.springframework.web.servlet.View. Some view resolver implementation are
BeanNameViewResolver, TilesViewResolver, UrlBasedViewResolver,
InternalResourceViewResolver
Let say we have placed all the jsp for application in view folder
Given the arrangement we have to configure InternalResourceViewResolver
in splitter-servlet.xml
When DS asks IRVR to resolve a view, it takes the logical name, prefix it with /WEB-
INF/views and suffixes it with .jsp, the path is then hands over to View object.
By default View object for IRVR is InternalResourceView, which dispatches request to jsp
Resolving View
ContextLoderListener
1. ContextLoderListener comes into play when project specific context files need to be
loaded apart from application context loaded by DispatcherServlet. To load below
snippet must be added in web.xml file.
2. By default ContextLoaderListener loads /WEB-INF/applicationContext.xml.
3. To specify one or more spring configuration files for ContextLoaderListener to load
we have to specify.
Handling Controller input
Controller which has to respond to URL which has user name as a request query
parameter.
Following shows an implementation that can respond to this kind of request.
Handling Controller input
1. Class level @RequestMapping defines the root level path that the class will handle.
2. Method level @RequestMapping narrows down the mapping defined in class level.
3. Attribute [method=GET] defined in @RequestMapping indicate method will handle
only get request.
4. @RequestParam is useful to bind query parameters where the name does not
match, If @RequestParam is not given then binding name will be same as request
paramter.
5. Under the cover the Model passed is Map<String, Object>, but model provide few
convenient method to populate the model.
Processing Form
Working with form in web application involve two step.
1. Processing from submission
2. Displaying the form
Let’s consider we are going to add createSpitterProfile method in SpitterController.
We have not specified path, so this method will be called for class level path specified.
It will handle url which has new in its query parameter.
The jsp form will have model attribute binded.
Processing Form input
After the form is submitted we will need a handler method that takes a spitter object
and saves it. Then the page should be redirected to user profile page.
Note addSpitterFromForm is adorns with @RequestMapping and does not have url path
means this method will handle class level url, this method is going to handle POST
method. And when the form is submitted the fields in the request will be bound to
the Spitter object that is passed in as an argument.
@Valid : This indicates the spitter should be passed before being processed.
Note : redirect :/ [helps to stop resubmission of form]
Processing Form input
Handling request with path variable.
The username portion of the path is actually a placeholder that correspond to method
parameter username that is annotated with @PathVAriable
Validating Input

More Related Content

PDF
Spring tutorial
PPS
JSP Error handling
PDF
M5 l7-rails controllers-response-handout
PDF
Share point review qustions
PDF
Murach: How to transfer data from controllers
PPTX
Http and REST APIs.
PDF
.NET Core, ASP.NET Core Course, Session 10
PPTX
Murach : HOW to work with controllers and routing
Spring tutorial
JSP Error handling
M5 l7-rails controllers-response-handout
Share point review qustions
Murach: How to transfer data from controllers
Http and REST APIs.
.NET Core, ASP.NET Core Course, Session 10
Murach : HOW to work with controllers and routing

What's hot (20)

PDF
Spring MVC 3.0 Framework (sesson_2)
PDF
.NET Core, ASP.NET Core Course, Session 18
DOCX
Spring review_for Semester II of Year 4
PDF
.NET Core, ASP.NET Core Course, Session 7
DOCX
LearningMVCWithLINQToSQL
PPT
Web Application Deployment
PDF
Murach: An introduction to web programming with ASP.NET Core MVC
PDF
Murach : How to develop a single-page MVC web
DOCX
Spring annotations notes
PDF
.NET Core, ASP.NET Core Course, Session 11
PPTX
Android MVVM architecture using Kotlin, Dagger2, LiveData, MediatorLiveData
DOC
JSP Scope variable And Data Sharing
PPS
Jsp element
PPTX
Jsp elements
PPTX
java Servlet technology
PDF
.NET Core, ASP.NET Core Course, Session 19
PPT
Programming Server side with Sevlet
PPTX
Servlet api &amp; servlet http package
PPTX
Parse template transformer mule
PDF
Transformation of Java Server Pages: A Modern Approach
Spring MVC 3.0 Framework (sesson_2)
.NET Core, ASP.NET Core Course, Session 18
Spring review_for Semester II of Year 4
.NET Core, ASP.NET Core Course, Session 7
LearningMVCWithLINQToSQL
Web Application Deployment
Murach: An introduction to web programming with ASP.NET Core MVC
Murach : How to develop a single-page MVC web
Spring annotations notes
.NET Core, ASP.NET Core Course, Session 11
Android MVVM architecture using Kotlin, Dagger2, LiveData, MediatorLiveData
JSP Scope variable And Data Sharing
Jsp element
Jsp elements
java Servlet technology
.NET Core, ASP.NET Core Course, Session 19
Programming Server side with Sevlet
Servlet api &amp; servlet http package
Parse template transformer mule
Transformation of Java Server Pages: A Modern Approach
Ad

Viewers also liked (20)

PDF
Programming with Threads in Java
PDF
What We Talk About When We Talk About Unit Testing
PPTX
Unidad 1 MOOC MVC
PDF
Become a Great Communicator
PPTX
Spoken english classes in kalewadi phata
PDF
Speak better English here's how.
PPTX
1st qtr 16 use variety of sentences
PDF
Spring mvc
PDF
Writing code you won't hate tomorrow
PPT
Threads in Java
PPTX
Learn english | Talk English
PDF
Basic java tutorial
PPTX
Mis ethical social
PPT
Threads in java
PPTX
PDF
Learning spoken Language.
PPTX
The truth of spoken english business
PPT
Speaking tips
PPT
Spring MVC Basics
ODP
Java Spring MVC Framework with AngularJS by Google and HTML5
Programming with Threads in Java
What We Talk About When We Talk About Unit Testing
Unidad 1 MOOC MVC
Become a Great Communicator
Spoken english classes in kalewadi phata
Speak better English here's how.
1st qtr 16 use variety of sentences
Spring mvc
Writing code you won't hate tomorrow
Threads in Java
Learn english | Talk English
Basic java tutorial
Mis ethical social
Threads in java
Learning spoken Language.
The truth of spoken english business
Speaking tips
Spring MVC Basics
Java Spring MVC Framework with AngularJS by Google and HTML5
Ad

Similar to Spring mvc (20)

PDF
Spring MVC Framework
PPTX
Spring MVC
ODP
servlet 2.5 & JSP 2.0
PPTX
Dispatcher
PDF
quickguide-einnovator-7-spring-mvc
PPT
PPTX
Spring mvc
PDF
Express node js
PPTX
Spring framework Controllers and Annotations
PDF
Mvc interview questions – deep dive jinal desai
ODP
springmvc-150923124312-lva1-app6892
PPTX
3. Spring MVC Intro - PowerPoint Presentation (1).pptx
PDF
Learn Drupal 8 Render Pipeline
PDF
Angular custom directives
PPT
2007 Zend Con Mvc
PPTX
Dyanaimcs of business and economics unit 2
PPTX
Intro react js
DOCX
Repository Pattern in MVC3 Application with Entity Framework
PPT
Node.js Express Framework
DOCX
Server side programming bt0083
Spring MVC Framework
Spring MVC
servlet 2.5 & JSP 2.0
Dispatcher
quickguide-einnovator-7-spring-mvc
Spring mvc
Express node js
Spring framework Controllers and Annotations
Mvc interview questions – deep dive jinal desai
springmvc-150923124312-lva1-app6892
3. Spring MVC Intro - PowerPoint Presentation (1).pptx
Learn Drupal 8 Render Pipeline
Angular custom directives
2007 Zend Con Mvc
Dyanaimcs of business and economics unit 2
Intro react js
Repository Pattern in MVC3 Application with Entity Framework
Node.js Express Framework
Server side programming bt0083

Recently uploaded (20)

PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
cuic standard and advanced reporting.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
KodekX | Application Modernization Development
PPTX
Cloud computing and distributed systems.
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Spectroscopy.pptx food analysis technology
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
cuic standard and advanced reporting.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Spectral efficient network and resource selection model in 5G networks
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation_ Review paper, used for researhc scholars
KodekX | Application Modernization Development
Cloud computing and distributed systems.
MIND Revenue Release Quarter 2 2025 Press Release
Network Security Unit 5.pdf for BCA BBA.
Big Data Technologies - Introduction.pptx
Spectroscopy.pptx food analysis technology
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
NewMind AI Weekly Chronicles - August'25 Week I
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Digital-Transformation-Roadmap-for-Companies.pptx

Spring mvc

  • 2. Topics  Mapping request to Spring controllers  Transparently binding form parameters.  Validating form submissions.  Uploading files.
  • 3. Request in MVC 1. DispatcherServlet is the first stop for request, 2. DS act as Front controller which delegates responsibility for a request to other component to perform actual processing. 3. DS job is to send request to Controller which DS does by the help of Handler mapping 4. At the controller the request drop off the payload(content send by user) and patiently wait while the controller process the information* 5. The controller pack the processed data in Model* and identifies the view to render. It then send the request along with model and view to DispatcherServlet. 6. The DS will consult the view resolver to map the logical view name to specified view implementation.
  • 4. Setting up Spring MVC 1. DispatcherServlet must be configured in web.xml 2. Because the servlet name is spitter DS will try to load spitter-servlet.xml 3. Next we must indicate what url will be handled by DS 4. We must also think of how the static resource should be handled, spring has new element for this purpose, Below content goes to spitter-servlet.xml 5. All our images, stylesheet, javascript need to be kept in resources folder.
  • 5. Writing a basic controller 1. Before we start with writing controller. Lets understand bit about HandlerMapping DispatcherServlet consult to find controller. 2. BeanNameUrlHandlerMapping, ConrollerBeanNameUrlMapping, ConrollerClassNameHanderMapping, DefaultAnnotationHandlerMapping, SimpleUrlHandlerMapping. 3. Using handler mapping is just about configuration. If no handler mapping is found then DispatcherServlet uses BeanNameUrlHandlerMapping and DefaultAnnotationHandlerMapping. 4. DefaultAnnotationHandlerMapping maps request to controller methods that are annotated with @RequestMapping . 5. Annotations make most of the task simple we will use it to bind request parameter to handle method parameters, perform validation and perform message conversion and we just have to configure a single line in splitter-servlet.xml. 1. ````````
  • 6. Writing a basic controller 1. @controller indicate this is a controller class. @controller is specialization of @component which means that <context:component-scan> will pick up and register it as bean. Which mean <context:componsent-scan> need to be configured in spitter- servlet.xml. 2. @Inject : Automatically inject bean when controller is instantiated
  • 7. Writing a basic controller 3. @RequestMapping : It serves two purpose. First, it identifies showHomePage() as request handling method for url pattern (/ or /home). Second, to render data to view* Model is populated with key value pair. It then return logical name. View resolver job is to map a logical view name to some implementation of org.springframework.web.servlet.View. Some view resolver implementation are BeanNameViewResolver, TilesViewResolver, UrlBasedViewResolver, InternalResourceViewResolver Let say we have placed all the jsp for application in view folder Given the arrangement we have to configure InternalResourceViewResolver in splitter-servlet.xml When DS asks IRVR to resolve a view, it takes the logical name, prefix it with /WEB- INF/views and suffixes it with .jsp, the path is then hands over to View object. By default View object for IRVR is InternalResourceView, which dispatches request to jsp Resolving View
  • 8. ContextLoderListener 1. ContextLoderListener comes into play when project specific context files need to be loaded apart from application context loaded by DispatcherServlet. To load below snippet must be added in web.xml file. 2. By default ContextLoaderListener loads /WEB-INF/applicationContext.xml. 3. To specify one or more spring configuration files for ContextLoaderListener to load we have to specify.
  • 9. Handling Controller input Controller which has to respond to URL which has user name as a request query parameter. Following shows an implementation that can respond to this kind of request.
  • 10. Handling Controller input 1. Class level @RequestMapping defines the root level path that the class will handle. 2. Method level @RequestMapping narrows down the mapping defined in class level. 3. Attribute [method=GET] defined in @RequestMapping indicate method will handle only get request. 4. @RequestParam is useful to bind query parameters where the name does not match, If @RequestParam is not given then binding name will be same as request paramter. 5. Under the cover the Model passed is Map<String, Object>, but model provide few convenient method to populate the model.
  • 11. Processing Form Working with form in web application involve two step. 1. Processing from submission 2. Displaying the form Let’s consider we are going to add createSpitterProfile method in SpitterController. We have not specified path, so this method will be called for class level path specified. It will handle url which has new in its query parameter. The jsp form will have model attribute binded.
  • 12. Processing Form input After the form is submitted we will need a handler method that takes a spitter object and saves it. Then the page should be redirected to user profile page. Note addSpitterFromForm is adorns with @RequestMapping and does not have url path means this method will handle class level url, this method is going to handle POST method. And when the form is submitted the fields in the request will be bound to the Spitter object that is passed in as an argument. @Valid : This indicates the spitter should be passed before being processed. Note : redirect :/ [helps to stop resubmission of form]
  • 13. Processing Form input Handling request with path variable. The username portion of the path is actually a placeholder that correspond to method parameter username that is annotated with @PathVAriable Validating Input