SlideShare a Scribd company logo
Spring Framework
&
Spring MVC
What's Spring MVC?
A model-view-controller framework for Java
web application
Made to simplify the writing and testing of
Java web applications
Fully integrates with the Spring dependency
injection (Inversion of Control) framework
Open Source
Developed and maintained by Interface21,
recently purchased by VMWare
Project Goals
J2EE should be easier to use
It is best to program to interfaces, rather than
classes. Spring reduces the complexity cost of using
interfaces to zero.
JavaBeans offer a great way of configuring
applications.
OO design is more important than any
implementation technology, such as J2EE.
Checked exceptions are overused in Java. A platform
shouldn't force you to catch exceptions you're
unlikely to be able to recover from.
Testability is essential, and a platform such as Spring
should help make your code easier to test.
Why Use Spring MVC?
• For most purposes you only have to define one
Servlet in web.xml
• Capable of Convention over Configuration Similar
to Ruby on Rails or other popular web frameworks
that work with dynamic languages
• Normal business objects can be used to back forms No
need to duplicate objects just to implement an MVC's
command object interface
• Very flexible view resolvers
• Can by used to map *.json, *.xml, *.atom, etc to the
same logic code in one controller and simply output
the type of data requested
• Enforces good Software Engineering principles like
SRP and DRY
Dispatcher Servlet
Used to handle all incoming requests and route
them through Spring
Uses customizable logic to determine which
controllers should handle which requests
Forwards all responses to through view handlers
to determine the correct views to route responses
to
Exposes all beans defined in Spring to controllers
for dependency injection
Dispatcher Servlet Architecture
Uses the Front Controller Design Pattern
Defining The Dispatcher Servlet
Defining a Dispatcher Servlet named "spring" that will intercept all
urls to this web application
Spring Configuration
By default Spring looks for a servletname -
servlet.xml file in /WEB-INF
For the previous example we would need to
create a file in /WEB-INF named spring-servlet.
xml
spring-servlet.xml
spring-servlet.xml cont.
<mvc:annotation-driven /> tells Spring to support annotations like
@Controller, @RequestMapping and others that simplify the
writing and configuration of controllers
spring-servlet.xml cont.
Define a simple view resolver that looks for JSPs that match a given
view name in the director /WEB-INF/jsp
spring-servlet.xml cont.
Tell Spring where to automatically detect controllers
Configuration Done!
So What's a Controller Look Like?
Example: Classroom Controller
A Controller that gets a class or all the students in the class
Mark this class as a controller
Define what default URLs this class should respond to
Side Note: Autowiring
Autowiring allows Spring to do the instantiation of the class you
want to make use of for you. At run time you will be able to access
all methods of the class without worrying about how you got the
class. This is known as Dependency Injection.
Back To Classroom Example
This method is the default method called when /classroom or / is
hit from a client. It simply forwards to a jsp named classroom.jsp
located in /WEB-INF/jsp
Side Note: Restful URLs
• Spring like many other popular frameworks can make use
of RESTful URLs
• They come in the style of /users/user_id
• Commonly without any extension such as .html
• Popularized by Ruby on Rails
• Collections are accessed like:
• /users
• Individual entries are accessed like:
• /users/user_id
• CRUD operations are done via HTTP
methods PUT, POST, GET, DELETE
Classroom RESTful URLs
The highlighted section above demonstrates how to accomplish
RESTful URLs in the Spring MVC Framework. Using the
@PathVariable annotation you can gain access to the variable
passed in on the URI. This is commonly referred to as URI
Templating.
What's a Model?
• A Model is used in Spring MVC to pass
objects from the controller tier up into the view.
• A Model is really just a java.util.Map
• You can add attributes to a Model and they
will be put on the request as attributes and
available in the applications PageContext .
• In Spring you can simply pass back a Map or
one of two Spring specific classes; ModelMap or
Model
ModelMap Example
In the above example we use a service method to read and return
a Classroom object. We make that Classroom object available to
the view under the key "classroom " by calling addAttribute() on
the ModelMap
Getting All Students In A Classroom
Above you can see that how to get all the students in a given
classroom by requesting the URL /classroom/{id} /students. A
Java List<Student> will be available to the classroom.jsp view for
display
Ad

More Related Content

Similar to Spring MVC framework features and concepts (20)

Spring Portlet MVC
Spring Portlet MVCSpring Portlet MVC
Spring Portlet MVC
John Lewis
 
Spring Framework
Spring FrameworkSpring Framework
Spring Framework
nomykk
 
quickguide-einnovator-7-spring-mvc
quickguide-einnovator-7-spring-mvcquickguide-einnovator-7-spring-mvc
quickguide-einnovator-7-spring-mvc
jorgesimao71
 
Session 1
Session 1Session 1
Session 1
Asif Atick
 
Laravel overview
Laravel overviewLaravel overview
Laravel overview
Obinna Akunne
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
Serhat Can
 
MVC Interview Questions PDF By ScholarHat
MVC Interview Questions PDF By ScholarHatMVC Interview Questions PDF By ScholarHat
MVC Interview Questions PDF By ScholarHat
Scholarhat
 
Asp.net mvc 5 course module 1 overview
Asp.net mvc 5 course   module 1 overviewAsp.net mvc 5 course   module 1 overview
Asp.net mvc 5 course module 1 overview
Sergey Seletsky
 
Jsp and Servlets
Jsp and ServletsJsp and Servlets
Jsp and Servlets
Raghu nath
 
Action-Domain-Responder: A Web-Specific Refinement of Model-View-Controller
Action-Domain-Responder: A Web-Specific Refinement of Model-View-ControllerAction-Domain-Responder: A Web-Specific Refinement of Model-View-Controller
Action-Domain-Responder: A Web-Specific Refinement of Model-View-Controller
Paul Jones
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
Harshit Choudhary
 
J2EE pattern 5
J2EE pattern 5J2EE pattern 5
J2EE pattern 5
Naga Muruga
 
MVC Framework
MVC FrameworkMVC Framework
MVC Framework
Ashton Feller
 
Jinal desai .net
Jinal desai .netJinal desai .net
Jinal desai .net
rohitkumar1987in
 
Asp 1a-aspnetmvc
Asp 1a-aspnetmvcAsp 1a-aspnetmvc
Asp 1a-aspnetmvc
Fajar Baskoro
 
Aspnetmvc 1
Aspnetmvc 1Aspnetmvc 1
Aspnetmvc 1
Fajar Baskoro
 
Sprint Portlet MVC Seminar
Sprint Portlet MVC SeminarSprint Portlet MVC Seminar
Sprint Portlet MVC Seminar
John Lewis
 
Module 5.ppt.............................
Module 5.ppt.............................Module 5.ppt.............................
Module 5.ppt.............................
Betty333100
 
Asp net mvc series for beginers part 1
Asp net mvc series for beginers part 1Asp net mvc series for beginers part 1
Asp net mvc series for beginers part 1
Gaurav Arora
 
ASPNet MVC series for beginers part 1
ASPNet MVC series for beginers part 1ASPNet MVC series for beginers part 1
ASPNet MVC series for beginers part 1
Gaurav Arora
 
Spring Portlet MVC
Spring Portlet MVCSpring Portlet MVC
Spring Portlet MVC
John Lewis
 
Spring Framework
Spring FrameworkSpring Framework
Spring Framework
nomykk
 
quickguide-einnovator-7-spring-mvc
quickguide-einnovator-7-spring-mvcquickguide-einnovator-7-spring-mvc
quickguide-einnovator-7-spring-mvc
jorgesimao71
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
Serhat Can
 
MVC Interview Questions PDF By ScholarHat
MVC Interview Questions PDF By ScholarHatMVC Interview Questions PDF By ScholarHat
MVC Interview Questions PDF By ScholarHat
Scholarhat
 
Asp.net mvc 5 course module 1 overview
Asp.net mvc 5 course   module 1 overviewAsp.net mvc 5 course   module 1 overview
Asp.net mvc 5 course module 1 overview
Sergey Seletsky
 
Jsp and Servlets
Jsp and ServletsJsp and Servlets
Jsp and Servlets
Raghu nath
 
Action-Domain-Responder: A Web-Specific Refinement of Model-View-Controller
Action-Domain-Responder: A Web-Specific Refinement of Model-View-ControllerAction-Domain-Responder: A Web-Specific Refinement of Model-View-Controller
Action-Domain-Responder: A Web-Specific Refinement of Model-View-Controller
Paul Jones
 
Sprint Portlet MVC Seminar
Sprint Portlet MVC SeminarSprint Portlet MVC Seminar
Sprint Portlet MVC Seminar
John Lewis
 
Module 5.ppt.............................
Module 5.ppt.............................Module 5.ppt.............................
Module 5.ppt.............................
Betty333100
 
Asp net mvc series for beginers part 1
Asp net mvc series for beginers part 1Asp net mvc series for beginers part 1
Asp net mvc series for beginers part 1
Gaurav Arora
 
ASPNet MVC series for beginers part 1
ASPNet MVC series for beginers part 1ASPNet MVC series for beginers part 1
ASPNet MVC series for beginers part 1
Gaurav Arora
 

Recently uploaded (20)

Vitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd year
Vitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd yearVitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd year
Vitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd year
ARUN KUMAR
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Diabetic neuropathy peripheral autonomic
Diabetic neuropathy peripheral autonomicDiabetic neuropathy peripheral autonomic
Diabetic neuropathy peripheral autonomic
Pankaj Patawari
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
Fundamentals of PR: Wk 4 - Strategic Communications
Fundamentals of PR: Wk 4 - Strategic CommunicationsFundamentals of PR: Wk 4 - Strategic Communications
Fundamentals of PR: Wk 4 - Strategic Communications
Jordan Williams
 
Open Access: Revamping Library Learning Resources.
Open Access: Revamping Library Learning Resources.Open Access: Revamping Library Learning Resources.
Open Access: Revamping Library Learning Resources.
Rishi Bankim Chandra Evening College, Naihati, North 24 Parganas, West Bengal, India
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Unit 4: Long term- Capital budgeting and its types
Unit 4: Long term- Capital budgeting and its typesUnit 4: Long term- Capital budgeting and its types
Unit 4: Long term- Capital budgeting and its types
bharath321164
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
Timber Pitch Roof Construction Measurement-2024.pptx
Timber Pitch Roof Construction Measurement-2024.pptxTimber Pitch Roof Construction Measurement-2024.pptx
Timber Pitch Roof Construction Measurement-2024.pptx
Tantish QS, UTM
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-26-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-26-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-26-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-26-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Vitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd year
Vitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd yearVitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd year
Vitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd year
ARUN KUMAR
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Diabetic neuropathy peripheral autonomic
Diabetic neuropathy peripheral autonomicDiabetic neuropathy peripheral autonomic
Diabetic neuropathy peripheral autonomic
Pankaj Patawari
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Fundamentals of PR: Wk 4 - Strategic Communications
Fundamentals of PR: Wk 4 - Strategic CommunicationsFundamentals of PR: Wk 4 - Strategic Communications
Fundamentals of PR: Wk 4 - Strategic Communications
Jordan Williams
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Unit 4: Long term- Capital budgeting and its types
Unit 4: Long term- Capital budgeting and its typesUnit 4: Long term- Capital budgeting and its types
Unit 4: Long term- Capital budgeting and its types
bharath321164
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
Timber Pitch Roof Construction Measurement-2024.pptx
Timber Pitch Roof Construction Measurement-2024.pptxTimber Pitch Roof Construction Measurement-2024.pptx
Timber Pitch Roof Construction Measurement-2024.pptx
Tantish QS, UTM
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Ad

Spring MVC framework features and concepts

  • 2. What's Spring MVC? A model-view-controller framework for Java web application Made to simplify the writing and testing of Java web applications Fully integrates with the Spring dependency injection (Inversion of Control) framework Open Source Developed and maintained by Interface21, recently purchased by VMWare
  • 3. Project Goals J2EE should be easier to use It is best to program to interfaces, rather than classes. Spring reduces the complexity cost of using interfaces to zero. JavaBeans offer a great way of configuring applications. OO design is more important than any implementation technology, such as J2EE. Checked exceptions are overused in Java. A platform shouldn't force you to catch exceptions you're unlikely to be able to recover from. Testability is essential, and a platform such as Spring should help make your code easier to test.
  • 4. Why Use Spring MVC? • For most purposes you only have to define one Servlet in web.xml • Capable of Convention over Configuration Similar to Ruby on Rails or other popular web frameworks that work with dynamic languages • Normal business objects can be used to back forms No need to duplicate objects just to implement an MVC's command object interface • Very flexible view resolvers • Can by used to map *.json, *.xml, *.atom, etc to the same logic code in one controller and simply output the type of data requested • Enforces good Software Engineering principles like SRP and DRY
  • 5. Dispatcher Servlet Used to handle all incoming requests and route them through Spring Uses customizable logic to determine which controllers should handle which requests Forwards all responses to through view handlers to determine the correct views to route responses to Exposes all beans defined in Spring to controllers for dependency injection
  • 6. Dispatcher Servlet Architecture Uses the Front Controller Design Pattern
  • 7. Defining The Dispatcher Servlet Defining a Dispatcher Servlet named "spring" that will intercept all urls to this web application
  • 8. Spring Configuration By default Spring looks for a servletname - servlet.xml file in /WEB-INF For the previous example we would need to create a file in /WEB-INF named spring-servlet. xml
  • 10. spring-servlet.xml cont. <mvc:annotation-driven /> tells Spring to support annotations like @Controller, @RequestMapping and others that simplify the writing and configuration of controllers
  • 11. spring-servlet.xml cont. Define a simple view resolver that looks for JSPs that match a given view name in the director /WEB-INF/jsp
  • 12. spring-servlet.xml cont. Tell Spring where to automatically detect controllers
  • 14. So What's a Controller Look Like?
  • 15. Example: Classroom Controller A Controller that gets a class or all the students in the class
  • 16. Mark this class as a controller
  • 17. Define what default URLs this class should respond to
  • 18. Side Note: Autowiring Autowiring allows Spring to do the instantiation of the class you want to make use of for you. At run time you will be able to access all methods of the class without worrying about how you got the class. This is known as Dependency Injection.
  • 19. Back To Classroom Example This method is the default method called when /classroom or / is hit from a client. It simply forwards to a jsp named classroom.jsp located in /WEB-INF/jsp
  • 20. Side Note: Restful URLs • Spring like many other popular frameworks can make use of RESTful URLs • They come in the style of /users/user_id • Commonly without any extension such as .html • Popularized by Ruby on Rails • Collections are accessed like: • /users • Individual entries are accessed like: • /users/user_id • CRUD operations are done via HTTP methods PUT, POST, GET, DELETE
  • 21. Classroom RESTful URLs The highlighted section above demonstrates how to accomplish RESTful URLs in the Spring MVC Framework. Using the @PathVariable annotation you can gain access to the variable passed in on the URI. This is commonly referred to as URI Templating.
  • 22. What's a Model? • A Model is used in Spring MVC to pass objects from the controller tier up into the view. • A Model is really just a java.util.Map • You can add attributes to a Model and they will be put on the request as attributes and available in the applications PageContext . • In Spring you can simply pass back a Map or one of two Spring specific classes; ModelMap or Model
  • 23. ModelMap Example In the above example we use a service method to read and return a Classroom object. We make that Classroom object available to the view under the key "classroom " by calling addAttribute() on the ModelMap
  • 24. Getting All Students In A Classroom Above you can see that how to get all the students in a given classroom by requesting the URL /classroom/{id} /students. A Java List<Student> will be available to the classroom.jsp view for display