Mike Calvo Citronella Software
Mike Calvo Citronella Software
Citronella Software
What is Spring MVC?
Web development framework part of Spring
Aims to simplify many of the tasks that are typically
cumbersome in Java web development:
Wiring requests to backend code
Mapping form parameters to backend values
Passing data to the view (JSP Page)
Testability of request handling code
Integration of request handling code with service layer
Spring MVC Core Classes
ContextLoaderListener
A web container listener that runs at context load
(startup) time that loads your application context and
puts it into a well defined location
DispatcherServlet
Rather than mapping a different servlet to each request,
this Spring-provided servlet is mapped to all handling
requests and forwards the request to the correct handler
High Level Approach
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-
class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Registering DispatcherServlet
1. Create a Servlet entry in your web.xml file with class
defined as
org.springframework.web.servlet.DispatcherServlet
(load-on-startup should be 1)
2. Create a Servlet mapping entry in your web.xml file
mapped to the DispatcherServlet with a pattern that will
match all handler code
Common examples include extension-based (*.do) or path-
based (/controller/*)
3. Create a spring application context file named
[servletname]-servlet.xml in your
src/main/webapp/WEB-INF folder
WebApplicationContext
The file WEB-INF/[servletname]-servlet.xml defines a
Spring application context specific to web request
handling
It can reference and inject beans defined in contexts
loaded by the ContextLoaderListener
Use it to define
Define Controllers, View Resolvers & Handler Mappings
Enable annotation-based Spring MVC features
Spring MVC Components
Controllers
The “C” in MVC
Interpret user input and transform it for service layer
processing
Prepares data for rendering by the “V”iew
Handler Mappings
Define the execution of controller code based on specific
URL mappings
View Resolvers
Converts logical names into view files for rendering
Controllers &View Mappings
Prior to Spring 2.5 implementing classes with specific
Interfaces was the only way to define
Spring 2.5 introduced an annotation-based approach
to defining them
Recommended approach is annotations for reduced
configuration and flexibility
Enabling Spring MVC-Annotations
Add the following to your [servletname]-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">