Introducing Spring MVC

Spring MVC support allows us to build flexible applications using an MVC Model 2 pattern. The implementation is truly generic, the model is a simple Map that holds the data, View is an interface whose implementations render the data, and the controller is an implementation of the Controller interface.

Introduction and Overview

Spring's implementation of the MVC architecture for web applications is based around DispatcherServlet. This servlet processes the requests and invokes appropriate controllers to handle the request.

The DispatcherServlet intercepts the incoming requests and determines which Controller will handle the request. The Spring controllers return a ModelAndView class from their handling methods. The ModelAndView instance holds a reference to a view and a model. The model is a simple Map instance that holds Java beans that the view is going to render. The View is an interface that, when implemented, defines the render method. It makes sense that the View implementation can be virtually anything the client can interpret.

Implementation

If we want to create a web application with Spring, we need to start with the basic web.xml, where we need to specify the DispatcherServlet and set the mapping for the specified url-pattern, as shown in Listing 17-1.

Listing 17-1: web.xml Descriptor

image from book
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"      "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app>     <display-name>Pro Spring Chapter 17 Sample application</display-name>     <description>dtto</description>          <servlet>         <servlet-name>ch171819</servlet-name>         <servlet-class>             org.springframework.web.servlet.DispatcherServlet         </servlet-class>         <load-on-startup>1</load-on-startup>     </servlet>     <servlet-mapping>         <servlet-name>ch17</servlet-name>         <url-pattern>*.html</url-pattern>     </servlet-mapping>     <servlet-mapping>         <servlet-name> ch171819</servlet-name>         <url-pattern>*.tile</url-pattern>     </servlet-mapping> </web-app>
image from book

This web.xml file defines the ch171819 servlet of the DispatcherServlet class that maps to all requests to *.html[1] or *.tile.

[1] I usually create mapping to *.html because it is a recognized extension and it easily fools the search engines into thinking that it is not a dynamically generated page.



Pro Spring
Pro Spring
ISBN: 1590594614
EAN: 2147483647
Year: 2006
Pages: 189

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net