SlideShare a Scribd company logo
1 of 24
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

More Related Content

Similar to Spring MVC framework features and concepts

Spring Portlet MVC
Spring Portlet MVCSpring Portlet MVC
Spring Portlet MVCJohn Lewis
 
Spring Framework
Spring FrameworkSpring Framework
Spring Frameworknomykk
 
quickguide-einnovator-7-spring-mvc
quickguide-einnovator-7-spring-mvcquickguide-einnovator-7-spring-mvc
quickguide-einnovator-7-spring-mvcjorgesimao71
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework Serhat Can
 
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 overviewSergey Seletsky
 
Jsp and Servlets
Jsp and ServletsJsp and Servlets
Jsp and ServletsRaghu 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-ControllerPaul Jones
 
Sprint Portlet MVC Seminar
Sprint Portlet MVC SeminarSprint Portlet MVC Seminar
Sprint Portlet MVC SeminarJohn Lewis
 
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 1Gaurav Arora
 
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 1Gaurav Arora
 
Java J2EE Interview Questions Part 2
Java J2EE Interview Questions Part 2Java J2EE Interview Questions Part 2
Java J2EE Interview Questions Part 2javatrainingonline
 

Similar to Spring MVC framework features and concepts (20)

Spring Portlet MVC
Spring Portlet MVCSpring Portlet MVC
Spring Portlet MVC
 
Spring Framework
Spring FrameworkSpring Framework
Spring Framework
 
quickguide-einnovator-7-spring-mvc
quickguide-einnovator-7-spring-mvcquickguide-einnovator-7-spring-mvc
quickguide-einnovator-7-spring-mvc
 
Session 1
Session 1Session 1
Session 1
 
Laravel overview
Laravel overviewLaravel overview
Laravel overview
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
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
 
Jsp and Servlets
Jsp and ServletsJsp and Servlets
Jsp and Servlets
 
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
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
J2EE pattern 5
J2EE pattern 5J2EE pattern 5
J2EE pattern 5
 
MVC Framework
MVC FrameworkMVC Framework
MVC Framework
 
Jinal desai .net
Jinal desai .netJinal desai .net
Jinal desai .net
 
Asp 1a-aspnetmvc
Asp 1a-aspnetmvcAsp 1a-aspnetmvc
Asp 1a-aspnetmvc
 
Aspnetmvc 1
Aspnetmvc 1Aspnetmvc 1
Aspnetmvc 1
 
Sprint Portlet MVC Seminar
Sprint Portlet MVC SeminarSprint Portlet MVC Seminar
Sprint Portlet MVC Seminar
 
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
 
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
 
Java J2EE Interview Questions Part 2
Java J2EE Interview Questions Part 2Java J2EE Interview Questions Part 2
Java J2EE Interview Questions Part 2
 
Java J2EE Interview Question Part 2
Java J2EE Interview Question Part 2Java J2EE Interview Question Part 2
Java J2EE Interview Question Part 2
 

Recently uploaded

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
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
 
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
 
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
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
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
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 

Recently uploaded (20)

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.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
 
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
 
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
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
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
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 

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