More Related Content Similar to Spring MVC 3.0 Framework (20) Spring MVC 3.0 Framework2. copyright © I-Admin
Objectives
Introduce Spring MVC Module
Learn about Spring MVC Components
(Dispatcher, Handler mapping, Controller,
View Resolver, View)
4. copyright © I-Admin
What is Spring?
Spring is light weight, open source framework begins
developed by Spring Source Company
Spring 1.0 was released in 2004
Spring is available on www.springframework.org
It provides support for JPA, Hibernate, Web services,
Schedulers, Ajax, Struts, JSF and many other
frameworks
5. copyright © I-Admin
Why use Spring?
Spring was created to reduce the complexity
observed in Java enterprise application development
Spring is organised into many modules that offer
various types of assistance to the developer in a host
of application areas
• Spring enables developers to develop enterprise-
class applications using POJOs
• Testing an application written with Spring is simple
because environment-dependent code is moved into
this framework
6. copyright © I-Admin
By the way, just what is MVC?
Model-View-Controller or MVC is an architectural
pattern used in development of applications.
– Model encapsulates the raw data
– Controller responds to events, and instruct the model and view to
perform actions based on events
– View render information supplied by model in a form suitable for
user interaction.
9. copyright © I-Admin
Spring MVC Components
DispatcherServlet responsible for intercepting the
request and dispatching for specific urls.
Controller responsible for processing user requests
and building appropriate model and passes it to the
view for rendering
View interface represents presentation logic and is
responsible for rendering content
ModelAndView class objects encapsulates view and
model linking
Model encapsulates the application data, will consist
of POJO
10. copyright © I-Admin
DispatcherServlet
Used to handle all incoming requests and route them
through Spring
Also responsible to initialize the frameworks
components which are used to process the request
at various stages
Uses customizable logic to determine which
controllers should handle which requests
Forwards all responses to through view handlers to
determine the correct views to route responses to
DispatcherServlet Architecture uses the Front
Controller Design Pattern
12. copyright © I-Admin
DispatcherServlet Mapping
Request that are to be handled by DispatcherServlet
are to be mapped using a URL mapping in web.xml
config file.
Each DispatcherServlet has its own
WebApplicationContext
13. copyright © I-Admin
DispatcherServlet in web.xml
Dispatcher Servlet named "spring" that will intercept all urls to this web
application
<servlet-mapping> tag indicates what URLs will be handled by the which
DispatcherServlet
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
14. copyright © I-Admin
Spring Configuration
By default Spring looks for a servletname -servlet.xml file in /WEB-INF
For the previous example we would need to create a file in /WEB-INF named
spring-servlet.xml
<beans xmlns …….>
<mvc:annotation-driven/>
<context:component-scan base-package="com.iadmin.spring" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
15. copyright © I-Admin
Spring Configuration cont..
<mvc:annotation-driven /> tells Spring to support
annotations like @Controller, @RequestMapping
and others that simplify the writing and configuration
of controllers
InternalResourceViewResolver resolve the view
names, looks for JSPs that match a given view
name in the director /WEB-INF/jsp
<context:component-scan...> tell Spring where to
automatically detect controllers
16. copyright © I-Admin
The ‘C’ in MVC
DispatcherServlet delegates the request to the
controllers to execute the functionality specific to it
The @Controller annotation indicates that a
particular class serves the role of a controller
Controller interpret user input and transform this
input into specific model which will be represented to
the user by the view
17. copyright © I-Admin
The ‘M’ in MVC
Model is generally defined as a MAP that can
contain objects that are to be displayed in view
ModelAndView object encapsulates the relations
between view and model and is returned by
corresponding Controller methods
ModelAndView class use ModelMap that is custom
MAP implementation where values are added in key-
value fashion
18. copyright © I-Admin
The ‘V’ in MVC
View Page can be explicitly returned as part of
ModelAndView object by the controller
In case of mapping logical name of view can be
resolved to particular view page in case
ModelAndView doesn’t contain the view reference
The view name can be independent of view
technology (without using .jsp in controller) and
resolved to specific technology by using
ViewResolver and render by View
19. copyright © I-Admin
Defining a Controller
@Controller annotation defines the class as a
Spring MVC controller
@RequestMapping annotation is used to map a
URL to either an entire class or a particular handler
method
DispatcherServlet delegates the request to the
controllers to execute the functionality specific to it
20. copyright © I-Admin
Defining a Controller cont…
@Controllerpublic
class HelloController{
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC !");
return "hello";
}
}
The value attribute indicates the URL to which the handler
method is mapped
The method attribute defines the service method to handle
HTTP GET request
21. copyright © I-Admin
Creating JSP Views
Spring MVC supports many types of views for different presentation
technologies. These include - JSPs, HTML, PDF, Excel worksheets, XML,
Velocity templates, XSLT, JSON, Atom and RSS feeds, JasperReports etc
/WEB-INF/hello/hello.jsp:
<html>
<head>
<title>Hello Spring MVC</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
Here, ${message} is the attribute which we have setup inside the Controller
22. copyright © I-Admin
Spring vs. Struts
Struts Framework
– Based on MVC architecture
– View – JSPs, ActionForms
– Controller – ActionServlet, Actions
Spring Framework
– DispatcherServlet - Spring’s Front Controller implementation
– View- JSP’s
– ViewResolver - Maps logical View names to actual View
implementations
– HandlerMapping-Strategy interface used by DispatcherServlet for
mapping incoming requests to individual Controllers