SlideShare a Scribd company logo
SPRING MVC
The spring web MVC provides model-view-controller
architecture
By Nagaraju
 The Model encapsulates the application data and in general they will consist of
POJO.
 The View is responsible for rendering the model data and in general it generates
HTML output that the client's browser can interpret.
 The Controller is responsible for processing user requests and building appropriate
model and passes it to the view for rendering.
The Spring web model-view-controller(MVC) is designed around a DispatcherServlet
that handles all the http request and responses. . The request processing workflow of the
Spring Web MVC DispatcherServlet is illustrated in the following diagram:
Following is the sequence of events corresponding to an incoming HTTP request to
DispatcherServlet.
 After receiving an HTTP request, DispatcherServlet consults the HandlerMapping to call the
appropriate Controller.
 The Controller takes the request and calls the appropriate service methods based on used GET
or POST method. The service method will set model data based on defined business logic and
returns view name to the DispatcherServlet.
 The DispatcherServlet will take help from ViewResolver to pickup the defined view for the
request.
 Once view is finalized, The DispatcherServlet passes the model data to the view which is
finally rendered on the browser.
Required Configuration:
You need to map requests that you want the DispatcherServlet to handle, by using a URL mapping
in the web.xml.
<servlet>
<servlet-name>ServletName</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping><servlet-name>ServletName</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
 Upon initialization of ServletName DispatcherServlet, the framework will try to load the
applicationcontext from a file named [servlet-name]-servlet.xml located in the application's
WebContent/WEB-INF directory.
 We can customize this file name and location by adding the servletlistenerContextLoaderListener in
your web.xml file as follows.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
 Required configuration for [servletname]-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package=“packagename" />
<bean ><property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Following are the important points about HelloWeb-servlet.xmlfile:
 The [servlet-name]-servlet.xml file will be used to create the beans defined, overriding the definitions of any beans
defined with the same name in the global scope.
 The <context:component-scan...> tag will be use to activate Spring MVC annotationscanning capability which allows to
make use of annotations like @Controller and @RequestMapping etc.
Defining a controller:
@Controller annotation indicates that a particular class serves the role of a controller.
@RequestMapping annotation is used to map a URL to either an entire class or a particular handler method.
Ex:
@Controller
@RequestMapping("/hello")
public class HelloController{
@RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
SPRING SECURITY
 We can do authentication and authorization for the some specific urls by using spring security.
Only selected people can use the urls.
 We will mention the url in the spirng-security.xml to which authentication is to be done and the
username and password.
 Create the Spring-Security.xml file and mention the url to which authentication is to be done in
the intercept-url tag like this as shown in below
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-
security-3.2.xsd">
<http auto-config="true">
<intercept-url pattern="/Url" access="ROLE_USER" /> </http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name=“userName" password=“Password" authorities="ROLE_USER" />
</user-service> </authentication-provider> </authentication-manager>
</beans:beans>
 Integrate Spring Security
To integrate Spring security with a Spring MVC web application, just
declares DelegatingFilterProxy as a servlet filter to intercept any Incoming request.
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
We have to include the these tag in the Web.xml
We can redirect the page based on the authentication status.
We can include our custom login page.
<form-login login-page="/login" default-target-url="/welcome"
authentication-failure-url="/login?error"
username-parameter="username" 3
password-parameter="password" />
<logout logout-success-url="/login?logout" />
Based on the authentication we can redirect to specific page.

More Related Content

What's hot (20)

PDF
SpringMVC
Akio Katayama
 
ODP
springmvc-150923124312-lva1-app6892
Tuna Tore
 
PPT
Spring MVC Basics
Bozhidar Bozhanov
 
PDF
Jsf intro
vantinhkhuc
 
PPT
Jsf2.0 -4
Vinay Kumar
 
PDF
Spring mvc
Guo Albert
 
PDF
Java server faces
Fábio Santos
 
PPTX
Introduction to jsf 2
yousry ibrahim
 
PPTX
Spring 3.x - Spring MVC - Advanced topics
Guy Nir
 
ODP
A Complete Tour of JSF 2
Jim Driscoll
 
PPT
MVC
akshin
 
PPT
JSF basics
airbo
 
PPT
Mvc architecture
Surbhi Panhalkar
 
PPT
Java Server Faces (JSF) - Basics
BG Java EE Course
 
PPT
Struts Introduction Course
guest764934
 
PDF
Spring MVC 3.0 Framework (sesson_2)
Ravi Kant Soni ([email protected])
 
PPT
Struts course material
Vibrant Technologies & Computers
 
PPT
JSF Component Behaviors
Andy Schwartz
 
PPTX
Jsp with mvc
vamsitricks
 
PPTX
Java server faces
owli93
 
SpringMVC
Akio Katayama
 
springmvc-150923124312-lva1-app6892
Tuna Tore
 
Spring MVC Basics
Bozhidar Bozhanov
 
Jsf intro
vantinhkhuc
 
Jsf2.0 -4
Vinay Kumar
 
Spring mvc
Guo Albert
 
Java server faces
Fábio Santos
 
Introduction to jsf 2
yousry ibrahim
 
Spring 3.x - Spring MVC - Advanced topics
Guy Nir
 
A Complete Tour of JSF 2
Jim Driscoll
 
MVC
akshin
 
JSF basics
airbo
 
Mvc architecture
Surbhi Panhalkar
 
Java Server Faces (JSF) - Basics
BG Java EE Course
 
Struts Introduction Course
guest764934
 
Spring MVC 3.0 Framework (sesson_2)
Ravi Kant Soni ([email protected])
 
Struts course material
Vibrant Technologies & Computers
 
JSF Component Behaviors
Andy Schwartz
 
Jsp with mvc
vamsitricks
 
Java server faces
owli93
 

Viewers also liked (13)

PDF
Reactive Streams and RxJava2
Yakov Fain
 
PDF
Live chym kysubrse vs toidicodedao
Huy Hoàng Phạm
 
PDF
Intro to Retrofit 2 and RxJava2
Fabio Collini
 
PDF
Từ Gà Đến Pro Git và GitHub trong 60 phút
Huy Hoàng Phạm
 
PPT
Lap trinh java hieu qua
Lê Anh
 
PPTX
Sinh viên IT học và làm gì để không thất nghiệp
Huy Hoàng Phạm
 
PDF
Luận văn tìm hiểu Spring
An Nguyen
 
PDF
Spring mvc
Ba Big
 
PPTX
Hành trình trở thành web đì ve lốp pơ
Huy Hoàng Phạm
 
PDF
Từ Sinh Viên IT tới Lập Trình Viên
Huy Hoàng Phạm
 
PDF
Effective java
Haeil Yi
 
PPTX
Effective Java
Brice Argenson
 
PPTX
Effective java
Emprovise
 
Reactive Streams and RxJava2
Yakov Fain
 
Live chym kysubrse vs toidicodedao
Huy Hoàng Phạm
 
Intro to Retrofit 2 and RxJava2
Fabio Collini
 
Từ Gà Đến Pro Git và GitHub trong 60 phút
Huy Hoàng Phạm
 
Lap trinh java hieu qua
Lê Anh
 
Sinh viên IT học và làm gì để không thất nghiệp
Huy Hoàng Phạm
 
Luận văn tìm hiểu Spring
An Nguyen
 
Spring mvc
Ba Big
 
Hành trình trở thành web đì ve lốp pơ
Huy Hoàng Phạm
 
Từ Sinh Viên IT tới Lập Trình Viên
Huy Hoàng Phạm
 
Effective java
Haeil Yi
 
Effective Java
Brice Argenson
 
Effective java
Emprovise
 
Ad

Similar to Spring mvc (20)

PPTX
Spring MVC
Emprovise
 
PDF
quickguide-einnovator-7-spring-mvc
jorgesimao71
 
PPTX
Spring mvc
Harshit Choudhary
 
PPT
Spring MVC 3.0 Framework
Ravi Kant Soni ([email protected])
 
PDF
Jinal desai .net
rohitkumar1987in
 
PDF
Spring MVC Framework
Hùng Nguyễn Huy
 
PPTX
Simple mvc4 prepared by gigin krishnan
Gigin Krishnan
 
PDF
Spring tutorial
Sanjoy Kumer Deb
 
PPT
ASP.NET MVC introduction
Tomi Juhola
 
PPS
Introduction To Mvc
Volkan Uzun
 
PDF
Mvc interview questions – deep dive jinal desai
jinaldesailive
 
PPT
Oip presentation
Harsha Halgaswatta
 
PDF
Introduction to Struts 1.3
Ilio Catallo
 
PPTX
3. Spring MVC Intro - PowerPoint Presentation (1).pptx
trevor8osborne
 
PDF
Spring mvc 2.0
Rudra Garnaik, PMI-ACP®
 
PPTX
Unit 38 - Spring MVC Introduction.pptx
AbhijayKulshrestha1
 
PPTX
Asp.net mvc
erdemergin
 
PPTX
Dispatcher
RAHUL VUTUKURI
 
PPTX
Spring Web MVC
zeeshanhanif
 
PDF
Spring Framework-II
People Strategists
 
Spring MVC
Emprovise
 
quickguide-einnovator-7-spring-mvc
jorgesimao71
 
Spring mvc
Harshit Choudhary
 
Spring MVC 3.0 Framework
Ravi Kant Soni ([email protected])
 
Jinal desai .net
rohitkumar1987in
 
Spring MVC Framework
Hùng Nguyễn Huy
 
Simple mvc4 prepared by gigin krishnan
Gigin Krishnan
 
Spring tutorial
Sanjoy Kumer Deb
 
ASP.NET MVC introduction
Tomi Juhola
 
Introduction To Mvc
Volkan Uzun
 
Mvc interview questions – deep dive jinal desai
jinaldesailive
 
Oip presentation
Harsha Halgaswatta
 
Introduction to Struts 1.3
Ilio Catallo
 
3. Spring MVC Intro - PowerPoint Presentation (1).pptx
trevor8osborne
 
Spring mvc 2.0
Rudra Garnaik, PMI-ACP®
 
Unit 38 - Spring MVC Introduction.pptx
AbhijayKulshrestha1
 
Asp.net mvc
erdemergin
 
Dispatcher
RAHUL VUTUKURI
 
Spring Web MVC
zeeshanhanif
 
Spring Framework-II
People Strategists
 
Ad

More from nagarajupatangay (8)

PPTX
Exception handling in mule
nagarajupatangay
 
PPTX
Java script object notation(json)
nagarajupatangay
 
PPTX
Web service
nagarajupatangay
 
PPTX
Billing and Revenue Management
nagarajupatangay
 
Exception handling in mule
nagarajupatangay
 
Java script object notation(json)
nagarajupatangay
 
Web service
nagarajupatangay
 
Billing and Revenue Management
nagarajupatangay
 

Recently uploaded (20)

PPTX
Applied-Statistics-1.pptx hardiba zalaaa
hardizala899
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
PPTX
Virus sequence retrieval from NCBI database
yamunaK13
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
PDF
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
PDF
My Thoughts On Q&A- A Novel By Vikas Swarup
Niharika
 
PPTX
Top 10 AI Tools, Like ChatGPT. You Must Learn In 2025
Digilearnings
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
Introduction to Probability(basic) .pptx
purohitanuj034
 
PPTX
Electrophysiology_of_Heart. Electrophysiology studies in Cardiovascular syste...
Rajshri Ghogare
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
PDF
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PPTX
LDP-2 UNIT 4 Presentation for practical.pptx
abhaypanchal2525
 
PPT
DRUGS USED IN THERAPY OF SHOCK, Shock Therapy, Treatment or management of shock
Rajshri Ghogare
 
PDF
John Keats introduction and list of his important works
vatsalacpr
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
Applied-Statistics-1.pptx hardiba zalaaa
hardizala899
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
Virus sequence retrieval from NCBI database
yamunaK13
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
My Thoughts On Q&A- A Novel By Vikas Swarup
Niharika
 
Top 10 AI Tools, Like ChatGPT. You Must Learn In 2025
Digilearnings
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
Introduction to Probability(basic) .pptx
purohitanuj034
 
Electrophysiology_of_Heart. Electrophysiology studies in Cardiovascular syste...
Rajshri Ghogare
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
LDP-2 UNIT 4 Presentation for practical.pptx
abhaypanchal2525
 
DRUGS USED IN THERAPY OF SHOCK, Shock Therapy, Treatment or management of shock
Rajshri Ghogare
 
John Keats introduction and list of his important works
vatsalacpr
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 

Spring mvc

  • 1. SPRING MVC The spring web MVC provides model-view-controller architecture By Nagaraju
  • 2.  The Model encapsulates the application data and in general they will consist of POJO.  The View is responsible for rendering the model data and in general it generates HTML output that the client's browser can interpret.  The Controller is responsible for processing user requests and building appropriate model and passes it to the view for rendering. The Spring web model-view-controller(MVC) is designed around a DispatcherServlet that handles all the http request and responses. . The request processing workflow of the Spring Web MVC DispatcherServlet is illustrated in the following diagram:
  • 3. Following is the sequence of events corresponding to an incoming HTTP request to DispatcherServlet.  After receiving an HTTP request, DispatcherServlet consults the HandlerMapping to call the appropriate Controller.  The Controller takes the request and calls the appropriate service methods based on used GET or POST method. The service method will set model data based on defined business logic and returns view name to the DispatcherServlet.  The DispatcherServlet will take help from ViewResolver to pickup the defined view for the request.  Once view is finalized, The DispatcherServlet passes the model data to the view which is finally rendered on the browser. Required Configuration: You need to map requests that you want the DispatcherServlet to handle, by using a URL mapping in the web.xml. <servlet> <servlet-name>ServletName</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping><servlet-name>ServletName</servlet-name> <url-pattern>*.jsp</url-pattern> </servlet-mapping>
  • 4.  Upon initialization of ServletName DispatcherServlet, the framework will try to load the applicationcontext from a file named [servlet-name]-servlet.xml located in the application's WebContent/WEB-INF directory.  We can customize this file name and location by adding the servletlistenerContextLoaderListener in your web.xml file as follows. <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/HelloWeb-servlet.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>  Required configuration for [servletname]-servlet.xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package=“packagename" />
  • 5. <bean ><property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans> Following are the important points about HelloWeb-servlet.xmlfile:  The [servlet-name]-servlet.xml file will be used to create the beans defined, overriding the definitions of any beans defined with the same name in the global scope.  The <context:component-scan...> tag will be use to activate Spring MVC annotationscanning capability which allows to make use of annotations like @Controller and @RequestMapping etc. Defining a controller: @Controller annotation indicates that a particular class serves the role of a controller. @RequestMapping annotation is used to map a URL to either an entire class or a particular handler method. Ex: @Controller @RequestMapping("/hello") public class HelloController{ @RequestMapping(method = RequestMethod.GET) public String printHello(ModelMap model) { model.addAttribute("message", "Hello Spring MVC Framework!"); return "hello"; } }
  • 6. SPRING SECURITY  We can do authentication and authorization for the some specific urls by using spring security. Only selected people can use the urls.  We will mention the url in the spirng-security.xml to which authentication is to be done and the username and password.  Create the Spring-Security.xml file and mention the url to which authentication is to be done in the intercept-url tag like this as shown in below <beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring- security-3.2.xsd"> <http auto-config="true"> <intercept-url pattern="/Url" access="ROLE_USER" /> </http> <authentication-manager> <authentication-provider> <user-service> <user name=“userName" password=“Password" authorities="ROLE_USER" /> </user-service> </authentication-provider> </authentication-manager> </beans:beans>
  • 7.  Integrate Spring Security To integrate Spring security with a Spring MVC web application, just declares DelegatingFilterProxy as a servlet filter to intercept any Incoming request. <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> We have to include the these tag in the Web.xml We can redirect the page based on the authentication status. We can include our custom login page. <form-login login-page="/login" default-target-url="/welcome" authentication-failure-url="/login?error" username-parameter="username" 3 password-parameter="password" /> <logout logout-success-url="/login?logout" /> Based on the authentication we can redirect to specific page.