0% found this document useful (0 votes)
34 views86 pages

1.0 Spring MVC

The document provides an overview of Spring MVC, emphasizing the Inversion of Control (IoC) principle and Dependency Injection as key features of the Spring framework. It explains the architecture of Spring MVC, including the role of the Front Controller, Handler Mapping, and View Resolver in processing web requests. Additionally, it outlines the steps for setting up a Spring MVC project in Eclipse, including modifying the web.xml deployment descriptor and creating necessary configuration files.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views86 pages

1.0 Spring MVC

The document provides an overview of Spring MVC, emphasizing the Inversion of Control (IoC) principle and Dependency Injection as key features of the Spring framework. It explains the architecture of Spring MVC, including the role of the Front Controller, Handler Mapping, and View Resolver in processing web requests. Additionally, it outlines the steps for setting up a Spring MVC project in Eclipse, including modifying the web.xml deployment descriptor and creating necessary configuration files.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 86

www.techmentor.co.

in
Spring MVC 2

05/05/25
Spring MVC 3

05/05/25
Traditional v/s Modern Behavior of Program

When any Traditional program starts -------

it starts the main program -----------

creates the objects and their couplings /


dependencies ----------

and then proceeds to execute the appropriate


methods.

Spring MVC 4

05/05/25
Reverse Mechanism
However, when we reverse the mechanism ----
the object creation dependencies and
relationships are created externally.
and then they are injected into the main program
as properties
and the program is then ready for action.

This is essentially the reverse of usual program


creation and hence is called Inversion of Control
principle
Spring MVC 5

05/05/25
Dependency Injection – Through IoC

Now a days ---------- SPRING framework is


really popular for its IoC Mechanism which is
applied through its feature
DEPENDENCY INJECTION.

This is the basic philosophy for SPRING


Framework.
Spring MVC 6

05/05/25
Understanding Inversion of Control in Spring

Actually IoC is known as Responsibility


Inversion Mechanism.
The IoC pattern inverts responsibility ---------
of managing some part of object life cycle
------------ from the application to the
framework ----------------- which makes writing
Java applications even easier.
Spring MVC 7

05/05/25
Understanding Inversion of Control

Consider the following example:

we have a Car class and a Vehicle class object.

public class Car {

private Vehicle vehicle;

public Car() { vehicle = new Vehicle() ; }

}
Spring MVC 8

05/05/25
Understanding Inversion of Control

The biggest issue with the code is tight


coupling between classes.

In other words, the Car class depends on the


vehicle object.

So, for any reason, changes in the Vehicle class


will lead to the changes in the Car class too.
Spring MVC 9

05/05/25
Understanding Inversion of Control

So let's put down the problems with this


approach:
The problem is that the Car class controls the
creation of the vehicle object .
The Vehicle class is directly referenced in the Car
class, which leads to tight coupling between the
car and vehicle objects .
Spring MVC 10

05/05/25
Understanding Inversion of Control

So if, for any reason, ---------- the vehicle


object could be not created, ------------
the whole Car class will fail in the constructor
initialization stage.

The basic principle of IoC stands on the basis


of the Hollywood Principle: ------------
Do not call us; we'll call you.
Spring MVC 11

05/05/25
Understanding Inversion of Control

Here ---------- In other words, ------------ it's like

----------- the Vehicle class saying to the Car


class, --------------------

"don't create me, I'll create myself using


someone else".

Spring MVC 12

05/05/25
Understanding Inversion of Control

The IoC framework can be ------------

a class, ---------------------- a client, ------------ or


some kind of IoC container.

The IoC container is a component ----

Which creates the vehicle object and


passes this reference to the Car class.
Spring MVC 13

05/05/25
Understanding Inversion of Control

Spring MVC 14

05/05/25
Spring MVC 15

05/05/25
Introduction to Spring MVC

Spring MVC module is based on two most


effective design patterns - Front controller and
traditional MVC.
We will now explore the entire architecture of
Spring MVC Framework.
Note that this MVC Pattern is explored from
Spring’s perspective.
Spring MVC 16

05/05/25
Front Controller Design Pattern
Invokes the Controller
Service Call
Service Layer
Controller
Request
Front Controller
Persistence
Model
Layer

Browser
Response
View

The user / client will interact with only one controller,


which is the “Front Controller”
Spring MVC 17

05/05/25
Architecture of Spring MVC

Spring MVC 18

05/05/25
Understanding Spring MVC
When an end user requests for Web Page ---------- By
putting some URL in the browser -------------
From Web Application side ----------- this is received by

----------- Front Controller.

Spring MVC 19

05/05/25
Understanding Spring MVC

This is the main component in Spring MVC


Module -----------

who controls the complete flow of the


application.

Off course ---------- it has many helper


components around it to get this work done.

Spring MVC 20

05/05/25
Understanding Spring MVC

Spring MVC 21

05/05/25
Understanding Spring MVC

Each of these helper component --------------

is going to do a certain specific task ----------- and


returns the desired result back to it.

With the help of all its helpers ------------ the Front


Controller’s job is
To prepare and send the response back to client

i.e HTML along with the data / content.


Spring MVC 22

05/05/25
Understanding Spring MVC

Immediately ----------- After getting the request ---

it passes the request to its foremost helper


component

Handler Mapping.

Its job is to tell the Front Controller ---------- what


this request is all about.

Spring MVC 23

05/05/25
Understanding Spring MVC

Spring MVC 24

05/05/25
Understanding Spring MVC

It will simply scan the URL ---------- and tell


Front Controller ------------

the complete address of another helper


component ------------

who can generate data for the web page


which an end user has requested for.
Spring MVC 25

05/05/25
Understanding Spring MVC

Based on the information provided by the


Handler Mapping ----------

the Front Controller passes on the request to


a specific component -------

Data Generator

Spring MVC 26

05/05/25
Spring MVC 27

05/05/25
Understanding Spring MVC

This Data Generator prepares the data --------

And typically ------------

Creates a Java Object to hold this data.

It may contain some business logic to


generate the required data

Spring MVC 28

05/05/25
Understanding Spring MVC

Or it may get ------ the data straight away


from DB.

Once it prepares the Java Object --------- it


sends it back to Front Controller.

Spring MVC 29

05/05/25
Understanding Spring MVC

Spring MVC 30

05/05/25
Understanding Spring MVC

In addition to the Java Object (which is holding


the data )----------

It sends one more thing to the Front Controller ---


And that is ------------- the name of the component
which is going to retrieve data from this Java Object

Along with mixing it with HTML to prepare the actual


response
Spring MVC 31

05/05/25
Understanding Spring MVC

Here the Front Controller is having a Java Object -

Which is to be consumed by some component


whose name is available

But not the actual address where it is located in


the application.

So it sends its name to another helper component


------------------ called as ------------- View Resolver.
Spring MVC 32

05/05/25
Spring MVC 33

05/05/25
Understanding Spring MVC

This View Resolver tells the exact address to the


Front Controller.

Now the Front Controller has a Java Object which


is holding the data.
As well as it also has the exact address of the component who
is going to generate the actual response.

Which is to be sent to client.

By putting up this data into an HTML.


Spring MVC 34

05/05/25
Understanding Spring MVC

So it sends Java Object to this component.

Now this component simply retrieves the data


from Java Object and puts it into HTML Code in
the desired fashion.

And then returns it to the Front Controller.

Eventually ------- Front Controller sends this as a


response to the client’s end.
Spring MVC 35

05/05/25
Understanding Spring MVC

Spring MVC 36

05/05/25
Understanding Spring MVC
This Final Response Builder is nothing but ----------
--------- a View Component.
The Java Object which is holding the data ----------
----- is a Model Component.
The component which returning the Model Object
along with the name of the view ------------
is a Controller Component.
Spring MVC 37

05/05/25
Understanding Spring MVC

The Front Controller in a Spring MVC Framework


is a class --------------

DispatcherServlet.

Here view component is typically a JSP Page.

Spring MVC 38

05/05/25
Installation and Setup for
SPRING MVC Application

using Eclipse IDE

Spring MVC 39

05/05/25
Installation and Setup using an Eclipse IDE

Let us create a fresh MVC Project with a clean


slate.

Right Click on Project Explorer

And choose --------- New ------------ Dynamic Web


Project ------------- Give a name to this project
----------- Next ----------- Check the Deployment
Descriptor Check Box ----------- Finish
Spring MVC 40

05/05/25
Installation and Setup using an Eclipse IDE

The Eclipse Project screen will appear with Project


Explorer showing the directory structure.

Now to use Spring MVC Framework we need to


have Spring MVC related JARs at project’s class
path.

We can have Spring Distributed Jars at ………


http:// repo.spring.io/release /org/springframework/spring --- Ver. 4.0.4
Spring MVC 41

05/05/25
Installation and Setup using an Eclipse IDE

Download the Required ZIP file and unzip it on


your local Hard Drive
spring-framework-4.0.4.RELEASE-dist

The Folder will have inner content like this ……

We will find all required


JARs in “libs” folder

Spring MVC 42

05/05/25
Installation and Setup using an Eclipse IDE

We will include all these JARs in our Spring MVC


Project’s classpath
i.e. in the project’s “lib” folder.

We can do this by just copying them into “lib”


folder.

So this way all the Spring MVC JAR files available


to our project.
Spring MVC 43

05/05/25
Installation and Setup using an Eclipse IDE

Additionally -------------- there is one more JAR


--------- which our Spring MVC Project would
need.

And that is ----------------- Commons-Logging.JAR

Spring MVC 44

05/05/25
Installation and Setup

We can download it from following Link

http://commons.apache.org/proper/
commons-logging/download_logging.cgi

https://commons.apache.org/proper/
commons-logging/download_logging.cgi

Spring MVC 45

05/05/25
Installation and Setup using an Eclipse IDE

Also we need to configure the Apache


Tomcat Server.

Spring MVC 46

05/05/25
Creating first Spring MVC Web Application

Eventually ----- create our first Spring MVC


project “FirstSpringMVCProject”.

Then add the required JARs to the project’s


classpath.

We will develop the Spring MVC Web Application


exactly according to the flow which we have
already explored.
Spring MVC 47

05/05/25
Creating first Spring MVC Web Application

Spring MVC 48

05/05/25
Creating first Spring MVC Web Application

Technically ---------

We just need to follow 4 simple steps to create basic


Spring MVC Web Application.

Modify web.xml

Create spring-dispatcher-servlet.xml

Create the HelloController.java class (A Controller)

Create the HelloPage.jsp file (A View)


Spring MVC 49

05/05/25
Modifying web.xml ---- Deployment Descriptor

Roll of Deployment Descriptor ------


The deployment descriptor is an xml file that
contains the basic and most important information
that is required to deploy a web application.
Without this, the web server would not know,
which requests to entertain/consider as requests
to access the servlet.

Spring MVC 50

05/05/25
Modifying web.xml ---- Deployment Descriptor

In any web project which is developed using


servlet technology ------------
We will always need to place the file web.xml under WEB-
INF Folder of the web peoject.

This file will actually map the input URL request to


an appropriate servlet which is placed there to serve
the request.
So called as a Deployment Descriptor.
Spring MVC 51

05/05/25
Modifying web.xml ---- Deployment Descriptor

The fact is that the Spring Framework is


actually using the servlet technology as its
base.

So ---------- In case of Spring MVC Project also

We need to have web.xml in WEB-INF


folder.
Spring MVC 52

05/05/25
Basic Structure of Deployment Descriptor
<web-app>
<servlet>
<servlet-name> ------ </servlet-name>
<servlet-class> ------ </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> ------ </servlet-name>
<url-pattern> ------ </url-pattern>
</servlet-mapping>
</web-app>

Spring MVC 53

05/05/25
Modifying web.xml

So we will modify the web.xml file which Eclipse


has already created ------------

This will be present in WebContent --> WEB-INF


folder

So open this web.xml file.

And make the following entries in this file

Spring MVC 54

05/05/25
WEB.XML – From Spring MVC viewpoint
<servlet> We have named the servlet as “spring-dispatcher ”.
<servlet-name> spring-dispatcher </servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name> spring-dispatcher </servlet-name>
<url-pattern> / </url-pattern>
</servlet-mapping>

The URI pattern in the servlet mapping section is “ / ”.

Thus all the requests matching the URI pattern will be handled by spring-dispatcher.

Spring MVC 55

05/05/25
Modifying web.xml
This XML code is simply telling the servlet technology
that
All URL requests to the web application should be
mapped to Spring MVC Framework for further
processing.
Here ------- we are specifically telling servlet
technology that ------------- Once you receive any URL
Request, just map the URL Request to something
called as a Front Controller.
Spring MVC 56

05/05/25
Modifying web.xml

Here DispatcherServlet is acting as a Front Controller.


And ---- after this ---------- the Front Controller will take the
complete responsibility of processing the URL Request
and sending back the response to the client side.

Off Course --------- it does this with the help of other


helper components around it.

The specified diagram shows this.

Spring MVC 57

05/05/25
Modifying web.xml

Spring MVC 58

05/05/25
Creating another XML Code ------ spring-dispatcher-servlet.xml

In order to make Front Controller work --------- and


take charge of controlling the overall application ---
We need to provide all relevant information to it in a
file ----------- spring-dispatcher-servlet.xml

This will also reside inside WEB-INF Folder.

And this is nothing but our second step


Creating spring-dispatcher-servlet.xml.
Spring MVC 59

05/05/25
Creating another XML Code ------ spring-dispatcher-servlet.xml

Let us do that ---------


Here in order to create this XML file within our
Spring MVC project -----------------
Right click the Project ---------- New ---------- XML
File
Give the name ---- spring-dispatcher-
servlet.xml.
Finish
Spring MVC 60

05/05/25
Creating another XML Code ------ spring-dispatcher-servlet.xml

We will discuss about all relevant information


within this XML file ------ i.e.

Which would be needed by the Front Controller


to manage this overall application.

Then after that we will discuss more about this


XML code.

Spring MVC 61

05/05/25
Creating another XML Code ------ spring-dispatcher-servlet.xml
1

<bean id = “HandlerMapping” class =


“org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping ” />
<bean name = “/welcome.html” class = HelloController” /> 2
<bean id = “ viewResolver”
class = “org . springframework . web . servlet . view . InternalResourceViewResolver “ >
<property name="prefix“ >
<value> / WEB-INF / </value>
3
</property>
<property name=“suffix“ >
<value> .jsp </value>
</property>
</bean>
Spring MVC 62

05/05/25
Creating another XML Code ------ spring-dispatcher-servlet.xml

We can see three sections ----------- <bean> elements.


Each of them is conveying a specific information to the front
controller.
<bean id = “HandlerMapping” class = “org . springframework . web . servlet .
handler . BeanNameUrlHandlerMapping ” />

This first element tells the front controller ---------


Which Handler Mapping to use
Whose job is to just scan the input URL and tell to Front
Controller --------- which data generator class to be called for
further processing the request.
Spring MVC 63

05/05/25
Creating another XML Code ------ spring-dispatcher-servlet.xml

<bean id = “ viewResolver”

class = “org . springframework . web . servlet . view .


InternalResourceViewResolver “ >

This third element tells the front controller ---------


Which View Resolver to use

Whose job is to just tell the exact location of the view


present in the project.

Spring MVC 64

05/05/25
Creating another XML Code ------ spring-dispatcher-servlet.xml

<bean name = “/welcome.html” class = HelloController” />

This second element -----------


Acts as a Controller i.e. Data Generator Class

Spring MVC 65

05/05/25
Creating other codes in this Web App.

Now let us observe the codes for third and fourth


step --------- i.e.

The Controller Class --------- HelloController.Java

The view code ---------------- HelloPage.jsp

Spring MVC 66

05/05/25
Spring MVC 67

05/05/25
WEB.XML – From Spring MVC viewpoint
We have named the servlet as “spring-dispatcher ”.
<display-name> FirstSpringMVCProject </display-name>
<servlet>
<servlet-name> spring-dispatcher </servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name> spring-dispatcher </servlet-name>
<url-pattern> / </url-pattern>
</servlet-mapping>

Spring MVC 68

05/05/25
Discussing the codes in this Web App – web.xml

Let us say the client requests for a web page using


following URL.
http: // localhost : 8989 / FirstSpringMVCProject / welcome.html.

Here the servlet technology would map this URL


request
to something called as DispatcherServlet which is
provided by Spring MVC Framework.
This DispatcherServlet is acting as a Front Controller.
Spring MVC 69

05/05/25 http://localhost:8585/FirstSpringMVCProject/welcome.html
Discussing the codes in this Web App – web.xml

This Front Controller would read a file with the name


spring-dispatcher-servlet.xml.

Then further it would control the application according to


the instructions written in spring-dispatcher-servlet.xml.

In simple words ----------- Once the control which is here in


XML ------- the Front Controller’s job is to prepare and send
back the response back to client.

Spring MVC 70

05/05/25
Discussing the codes in this Web App – web.xml

According to the flow diagram --------

The Front Controller first needs to find out --- which


controller class to be called with the help of Handler
Mapping classes provided by Spring MVC framework.

This is exactly what is exactly happening in the XML file


spring-dispatcher-servlet.xml.

Spring MVC 71

05/05/25
Discussing – spring-dispatcher-servlet.xml
<bean id = “HandlerMapping” class = “org . springframework . web . servlet .
handler . BeanNameUrlHandlerMapping ” />
<bean name = “/welcome.html” class = “HelloController” />

With help of this Handler Mapping class


Which is provided by Spring MVC Framework

The Front Controller makes a decision to call the HelloController


class.

So now the question is ---------- “How would it does this ???”

Spring MVC 72

05/05/25
Discussing – spring-dispatcher-servlet.xml
http : // localhost : 8989 / FirstSpringMVCProject

The Handler Mapping class would match the incoming request’s


URL pattern
with bean’s name of the controller classes which we have provided
here in spring-dispatcher-servlet.xml

<bean name = “/welcome.html” class = “HelloController” />

Spring MVC 73

05/05/25
Discussing – spring-dispatcher-servlet.xml

And once -------- it finds any bean’s name pattern is


matching with the incoming request URL pattern ---
It just suggests the Front Controller to make a call to that
controller class
<bean name = “/welcome.html” class = “HelloController” />

This has exactly happened here.

Spring MVC 74

05/05/25
Discussing – spring-dispatcher-servlet.xml
<bean name = “/welcome.html” class = “HelloController” />

After knowing that incoming request URL Pattern is


exactly matching with the bean’s name -----------
Which is for HelloController class.

the handler mapping class just suggests the Front


Controller to make a call to HelloController class

Spring MVC 75

05/05/25
Discussing – HelloController.Java

Now ones the control reaches here -------- in the


HelloController.Java class -------

We are simply setting the data which is to be displayed on the


client as a response.

Spring MVC 76

05/05/25
Here we are setting the data which is to be displayed as response

Here welcomeMessage is a variable Here HelloPage is a view


As a parameter to the addObject() name.
method Finally --- we are returning
Another parameter is actual message back ModelAndView object
to be send as response. to the Front Controller.

Spring MVC 77

05/05/25
Here we are setting the data which is to be displayed as response

Here each controller class has to be extended from some base


controller class.
Here we are extending this controller class from AbstractController class which
is a basic controller class provided by Spring MVC Framework.
Here we are overriding the methods from it

Spring MVC 78

05/05/25
Here we are setting the data which is to be displayed as response

Here ----- we are returning back the ModelAndView object back to


front controller.
Once the Front Controller receives the ModelAndView object
Now it will need to find out the exact location of the view present in the system
Using View Resolver classes provided by Spring MVC Framework.

Spring MVC 79

This will exactly happen further


05/05/25
The third <bean> component

This View Resolver class


Will prepare the exact location of the view which is present in the
project ---------
Using the prefix and suffix values which we mentioned here.
They can be mapped as follows

Spring MVC 80

05/05/25
Spring MVC 81

05/05/25
So based on this exact address ------- provided by this View
Resolver class

InternalResourceViewResolver class.

The front controller makes a call to the HelloPage.JSP

Spring MVC 82

05/05/25
Once the control reaches here
To the HelloPage.JSP ----------
Here we are simply retrieving the value of welcomeMessage variable
Which we have already set in spring-dispatcher-servlet.xml
Now we are mixing the value of welcomeMessage variable in HTML
This will be sent back to the client as a response.
Spring MVC 83

05/05/25
Once the response is prepared at this file ---------
It is sent to the Front Controller

And the Front Controller simply sends it back to Client.

So this completes our Basic Spring MVC Projet

Spring MVC 84

05/05/25
Spring MVC 85

05/05/25
Discussing – spring-dispatcher-servlet.xml
One more important point about Handler Mapping
class.
Actually ----- there are many Handler Mapping classes
provided by Spring MVC Framework.
Here ------- we are using the basic one.
BeanNameUrlHandlerMapping class

Each Handler Mapping class analyzes and matches


incoming request URL pattern using different approaches.
Spring MVC 86

05/05/25

You might also like