2160707
Advanced Java
Unit-7
Spring MVC
Prof. Swati R. Sharma
[email protected] Subject Overview
Sr. No. Unit % Weightage
1 Java Networking 5
2 JDBC Programming 10
3 Servlet API and Overview 25
4 Java Server Pages 25
5 Java Server Faces 10
6 Hibernate 15
7 Java Web Frameworks: Spring MVC 10
Reference Book:
Black Book “ Java server programming” J2EE, 1st ed., Dream Tech Publishers,
2008. 3. Kathy walrath ”
Chapter 21
Unit-7 Spring MVC 2 Darshan Institute of Engineering & Technology
What is MVC?
3
Unit-5 Java Server Faces 3 Darshan Institute of Engineering & Technology
MVC – An overview
Model
Controller
View
Response
Unit-7 Spring MVC 4 Darshan Institute of Engineering & Technology
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
Unit-7 Spring MVC 5 Darshan Institute of Engineering & Technology
MVC Architecture
Client’s Request Controller
Login
Login
Logout Logout
web.xml
Add
Add
Multiply
Multiply
Unit-7 Spring MVC 6 Darshan Institute of Engineering & Technology
Spring MVC Architecture
Controller
Client’s Request
Login
Login
Logout Front Logout
web.xml
Add Controller
Add
Multiply
Multiply
Unit-7 Spring MVC 7 Darshan Institute of Engineering & Technology
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.
Unit-7 Spring MVC 8 Darshan Institute of Engineering & Technology
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.
Unit-7 Spring MVC 9 Darshan Institute of Engineering & Technology
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.
Unit-7 Spring MVC 10 Darshan Institute of Engineering & Technology
Spring 3 MVC- Basic Architecture
HandlerMapping
(Map of URL and controllers)
2
Dispatcher 3
Controller
Request (Responsible to
1 Servlet handle request)
(Front controller) 4
5
View (JSP,
Model
(POJO)
XML,
Velocity)
Unit-7 Spring MVC 11 Darshan Institute of Engineering & Technology
Spring 3.0 MVC Request Flow
Request
Response
Dispatcher Servlet
Capture the Request Interceptor -
Locale Pre Process
Interceptor -
If request is Pre Process
multipart- File upload
data is exposed Handler Controller Prepare
the View
Chain Interceptor -
Post Process
HandlerMapping
(Map of URL and controllers) Interceptor -
Post Process View
Resolver
Unit-7 Spring MVC 12 Darshan Institute of Engineering & Technology
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.
Unit-7 Spring MVC 13 Darshan Institute of Engineering & Technology
Features of Spring MVC Framework
It is used to provide object
1. Inversion of Control (IoC) Container reference to class during
runtime.
2. Data Access Framework It enables developers to easily
write code to access the persistant
data throughout the application.
3. Transaction Management
It enables developers to model a
wide range of transaction by
4. Spring Web Services providing Java Transaction API (JTA).
It provides powerful mapping for transmitting
incoming XML request to any object.
Unit-7 Spring MVC 14 Darshan Institute of Engineering & Technology
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
Unit-7 Spring MVC 15 Darshan Institute of Engineering & Technology
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.
Unit-7 Spring MVC Darshan Institute of Engineering & Technology
Without Dependency-Injection/IoC
creates Object B
Object A
creates
Object C
An object creating its dependencies without IoC leads to tight object
coupling.
Unit-7 Spring MVC Darshan Institute of Engineering & Technology
With Dependency-Injection/IoC
Allows objects to be created at higher levels and passed into object so they can
use the implementation directly
Object B setB(IB)
Object A
setC(IC)
Object C
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.
Unit-7 Spring MVC Darshan Institute of Engineering & Technology
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.)
Unit-7 Spring MVC 19 Darshan Institute of Engineering & Technology
Important Intefaces
Interface Default bean name purpose
org.springframework.web.servlet. handlerMapping Maps the Request to
HandlerMapping Handlers(Controllers)
org.springframework.web.servlet. none Plugs the other frameworks
HandlerAdapter handlers
org.springframework.web.servlet. viewResolver Maps the view names to
ViewResolver view instances
org.springframework.web.servlet. handlerExceptionResolv Mapping of the exceptions to
HandlerExceptionResolver er handlers and views
org.springframework.web. multipartResolver Interface to handle the file
multipart.MultipartResolver uploads
org.springframework.web.servlet. localeResolver Helps to resolve the locale
LocaleResolver from the request
org.springframework.web.servlet. themeResolver Resolves a theme for a
ThemeResolver Request.
Unit-7 Spring MVC 20 Darshan Institute of Engineering & Technology
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.
Unit-7 Spring MVC 21 Darshan Institute of Engineering & Technology
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.
Unit-7 Spring MVC 22 Darshan Institute of Engineering & Technology
Java Bean Architecture
1
Browser
Request
JSP Pages
4
Response
2
3
Java Bean
Enterprise
Servlet Container Information
System (EIS)
Unit-7 Spring MVC 23 Darshan Institute of Engineering & Technology
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.
Unit-7 Spring MVC 24 Darshan Institute of Engineering & Technology
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."); }}
Unit-7 Spring MVC Darshan Institute of Engineering & Technology
GTU Questions
1. Explain MVC Architecture
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.
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.
Unit-7 Spring MVC 26 Darshan Institute of Engineering & Technology