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

More Related Content

PDF
Spring MVC
Aaron Schram
 
PDF
quickguide-einnovator-7-spring-mvc
jorgesimao71
 
ODP
springmvc-150923124312-lva1-app6892
Tuna Tore
 
ODP
Java Spring MVC Framework with AngularJS by Google and HTML5
Tuna Tore
 
PDF
Spring mvc
Guo Albert
 
PPT
Spring MVC 3.0 Framework
Ravi Kant Soni ([email protected])
 
PPTX
3. Spring MVC Intro - PowerPoint Presentation (1).pptx
trevor8osborne
 
PPTX
Spring mvc
Pravin Pundge
 
Spring MVC
Aaron Schram
 
quickguide-einnovator-7-spring-mvc
jorgesimao71
 
springmvc-150923124312-lva1-app6892
Tuna Tore
 
Java Spring MVC Framework with AngularJS by Google and HTML5
Tuna Tore
 
Spring mvc
Guo Albert
 
Spring MVC 3.0 Framework
Ravi Kant Soni ([email protected])
 
3. Spring MVC Intro - PowerPoint Presentation (1).pptx
trevor8osborne
 
Spring mvc
Pravin Pundge
 

Similar to Spring MVC framework features and concepts (20)

PPTX
Spring Web MVC
zeeshanhanif
 
PDF
Design & Development of Web Applications using SpringMVC
Naresh Chintalcheru
 
PPTX
Spring Web Presentation 123143242341234234
horiadobrin
 
PPTX
Spring framework in depth
Vinay Kumar
 
PDF
Toms introtospring mvc
Guo Albert
 
PDF
Spring tutorial
Sanjoy Kumer Deb
 
PDF
Spring MVC introduction HVA
Peter Maas
 
ODP
Spring Mvc,Java, Spring
ifnu bima
 
PDF
Spring mvc 2.0
Rudra Garnaik, PMI-ACP®
 
PDF
Multi Client Development with Spring - Josh Long
jaxconf
 
PDF
Spring Framework-II
People Strategists
 
KEY
Multi Client Development with Spring
Joshua Long
 
PPT
Spring Framework
nomykk
 
PPTX
Spring Framework
tola99
 
PPTX
Spring mvc
nagarajupatangay
 
PPTX
Spring MVC
Emprovise
 
PPTX
A project on spring framework by rohit malav
Rohit malav
 
PPT
Spring-training-in-bangalore
TIB Academy
 
PPTX
Spring MVC 5 & Hibernate 5 Integration
Majurageerthan Arumugathasan
 
PPTX
Spring
JyothirmaiG4
 
Spring Web MVC
zeeshanhanif
 
Design & Development of Web Applications using SpringMVC
Naresh Chintalcheru
 
Spring Web Presentation 123143242341234234
horiadobrin
 
Spring framework in depth
Vinay Kumar
 
Toms introtospring mvc
Guo Albert
 
Spring tutorial
Sanjoy Kumer Deb
 
Spring MVC introduction HVA
Peter Maas
 
Spring Mvc,Java, Spring
ifnu bima
 
Spring mvc 2.0
Rudra Garnaik, PMI-ACP®
 
Multi Client Development with Spring - Josh Long
jaxconf
 
Spring Framework-II
People Strategists
 
Multi Client Development with Spring
Joshua Long
 
Spring Framework
nomykk
 
Spring Framework
tola99
 
Spring mvc
nagarajupatangay
 
Spring MVC
Emprovise
 
A project on spring framework by rohit malav
Rohit malav
 
Spring-training-in-bangalore
TIB Academy
 
Spring MVC 5 & Hibernate 5 Integration
Majurageerthan Arumugathasan
 
Spring
JyothirmaiG4
 
Ad

Recently uploaded (20)

PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PDF
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
PPTX
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PDF
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
PDF
Sunset Boulevard Student Revision Booklet
jpinnuck
 
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PDF
Wings of Fire Book by Dr. A.P.J Abdul Kalam Full PDF
hetalvaishnav93
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PPTX
Strengthening open access through collaboration: building connections with OP...
Jisc
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PDF
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
PDF
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
PDF
Landforms and landscapes data surprise preview
jpinnuck
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
Sunset Boulevard Student Revision Booklet
jpinnuck
 
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
Wings of Fire Book by Dr. A.P.J Abdul Kalam Full PDF
hetalvaishnav93
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
Strengthening open access through collaboration: building connections with OP...
Jisc
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
Landforms and landscapes data surprise preview
jpinnuck
 
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