2. 2
MVC – An overview
Controller
View
Model
Response
3. 3
Advantage of MVC
Navigation control is centralized. Now only controller contains the
logic to determine the next page.
Easy to maintain
Easy to extend
Easy to test
Better separation of concerns
Reusability
6. 6
Front Controller -
Responsibilities
Initialize the framework to supply to the requests.
Load the map of all the URLs and the components responsible to
handle the request.
Prepare the map for the views.
7. 7
What is Spring MVC Framework?
Spring links objects together instead of the objects linking
themselves together.
Spring object linking is defined in XML files, allowing easy changes
for different application configurations thus working as a plug in
architecture.
8. 8
What is Spring MVC Framework?
In an MVC architecture the controllers handle all requests.
Spring uses a “DispatcherServlet” defined in the web.xml file to
analyze a request URL pattern and then pass control to the correct
controller by using a URL mapping defined in a “Spring bean” XML
file.
9. 9
Spring 3 MVC- Basic Architecture
Dispatcher
Servlet
(Front
controller)
HandlerMapping
(Map of URL and controllers)
Controller
(Responsible to
handle request)
View (JSP,
XML,
Velocity)
Model
(POJO)
Request
1
5
4
3
2
10. 10
Spring 3.0 MVC Request Flow
Dispatcher Servlet
Capture the Request
Locale
If request is
multipart- File upload
data is exposed
HandlerMapping
(Map of URL and controllers)
Handler
Chain
Interceptor -
Pre Process
Interceptor -
Pre Process
Controller
Interceptor -
Post Process
Interceptor -
Post Process
View
Resolver
Prepare
the View
Request
Response
11. 11
Why Spring Framework?
All frameworks integrate well with Spring.
Consistent Configuration, open plug-in architecture.
Integrates well with different O/R Mapping frameworks like
Hibernate.
Easier to test applications with.
Less complicated then other frameworks.
Active user community.
Spring is well organized and seems easier to learn comparatively
Spring also supports JDBC Framework that makes it easier to
create JDBC Apps.
12. 12
Features of Spring MVC
Framework
1. Inversion of Control (IoC) Container
2. Data Access Framework
3. Transaction Management
4. Spring Web Services
It is used to provide object
reference to class during
runtime.
It enables developers to easily write
code to access the persistant data
throughout the application.
It enables developers to model a
wide range of transaction by
providing Java Transaction API (JTA).
It provides powerful mapping for transmitting
incoming XML request to any object.
13. 13
Advantage of Spring MVC
Framework
Predefined Templates
Loose Coupling
Easy to test
Lightweight
Fast Development
Declarative Support
Hibernate and JDBC Support
MVC Architecture and JavaBean Support
14. Example of web.xml file
<web-app>
<servlet>
<servlet-name>tradingapp</servlet-name>
<servlet-class>DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>tradingapp</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
*** Any URL ending with an “.html” pattern is routed to the
DispatcherServlet, the DispatcherServlet loads the tradingapp-servlet.xml
file and routes the user to the correct controller.
16. Object A
Object B
Object C
setB(IB)
setC(IC)
Object A contains setter methods that accept interfaces to objects B
and C. This could have also been achieved with constructors in
object A that accepts objects B and C.
With Dependency-Injection/IoC
Allows objects to be created at higher levels and passed into object so they can
use the implementation directly
17. Overview of Spring
Spring is a dependency injection framework to make java application loosely coupled.
Spring framework makes the easy development of JavaEE application.
What is Spring?
Dependency Injection (DI) is a design pattern used to implement IoC.
It allows the creation of dependent objects outside of a class and provides
those objects to a class through different ways.
Using DI, we move the creation and binding of the dependent objects outside
of the class that depends on them.
Dependency Injection
18. Overview of Spring
Client Service
Injector
Uses
Injects
The Dependency Injection pattern involves 3 types of classes.
1) Client Class: The client class (dependent class) is a class which depends on the service
class
2) Service Class: The service class (dependency) is a class that provides service to the client
class.
3) Injector Class: The injector class injects the service class object into the client class.
19. 19
Advantage of Spring MVC over
MVC
Spring is a powerful Java application framework, used in a wide
range of Java applications.
Spring provides a very clean division between controllers,
JavaBean models, and views.
Spring’s MVC is very flexible.
Spring MVC is entirely based on interfaces.
Every part of the Spring MVC framework is configurable via
plugging in your own interface.
No Action Forms, bind directly to domain objects.
More testable code (validation has no dependency on Servlet
API).
Spring offers better integration with view technologies other than
JSP (Velocity / XSLT / FreeMarker / XL etc.)
20. 20
Important Intefaces
Interface Default bean name purpose
org.springframework.web.servlet.
HandlerMapping
handlerMapping Maps the Request to
Handlers(Controllers)
org.springframework.web.servlet.
HandlerAdapter
none Plugs the other frameworks
handlers
org.springframework.web.servlet.
ViewResolver
viewResolver Maps the view names to
view instances
org.springframework.web.servlet.
HandlerExceptionResolver
handlerExceptionResolv
er
Mapping of the exceptions to
handlers and views
org.springframework.web.
multipart.MultipartResolver
multipartResolver Interface to handle the file
uploads
org.springframework.web.servlet.
LocaleResolver
localeResolver Helps to resolve the locale
from the request
org.springframework.web.servlet.
ThemeResolver
themeResolver Resolves a theme for a
Request.
21. 21
Java Bean vs Basic Java Class
A Bean is a Java class, but a Java class does not have to be a bean.
A Java Bean is a java class that should follow following
conventions:
1. It should have a no-arg constructor.
2. It should be Serializable.
3. It should provide methods to set and get the values of the properties,
known as getter and setter methods.
22. 22
Why use Java Bean?
It is a reusable software component.
A bean encapsulates many objects into one object, so we can
access this object from multiple places.
Moreover, it provides the easy maintenance.
24. 24
Bean Life Cycle
The life cycle of a Spring bean is easy to understand.
1. When a bean is instantiated, it may be required to perform
some initialization to get it into a usable state.
2. Similarly, when the bean is no longer required and is removed
from the container, some cleanup may be required.
25. Bean Life Cycle
1. public class HelloWorld {
2. private String message;
3. public void init(){
4. System.out.println("Bean is going through init.");}
5. public void setMessage(String message){
6. this.message = message; }
7. public void getMessage(){
8. System.out.println("Your Message : " + message);}
9. public void destroy(){
10. System.out.println("Bean will destroy now."); }}
26. 26
Questions
1. Explain MVC Architecture
5. Explain MVC architecture in detail with figure.
6. Write a java bean named “student” having roll no and name having getter &
setter methods. Write a JSP page to set the roll number and name for a
student object and then print them by reading from object.
7. What is MVC architecture? Explain Spring architecture with neat
sketch.
8. What is Dependency Injection?
9. Briefly explain spring bean life cycle.
2. What are the differences between Java Bean and basic java class? Explain
Java Bean Architecture.
3. Differentiate : Java Bean and basic java class and Explain Java Bean
Architecture and Show the use of JSP inbuilt objects: request and response,
with their use in application
4. What is Spring Web MVC framework? List its key features.