lishman levelup
«previous  next»

Getting Started
Form Processing
IoC Container
Hibernate ORM

Spring MVC Configuration

Spring uses an implementation of the front controller pattern, called DispatcherServlet, to dispatch HTTP requests to the appropriate controller.


Here we register a DispatcherServlet called 'world' in the web.xml file, and map any URLs that end in .html to this servlet.

  <!-- A servlet that dispatches requests to registered controllers -->

  <!--  Send all .html files to the Spring dispatcher servlet -->

  <!-- Define the web application entry point -->


Servlet Configuration File

During startup, Spring will look for a configuration file called [servlet-name]-servlet.xml in the WEB-INF directory of the web application. In our case, the file name is world-servlet.xml and it contains this information:

  <!-- Auto-detect controllers in this package -->
  <context:component-scan base-package=""/>

  <!--  Prepend /WEB-INF/jsp/ and append .jsp to the view name  -->
  <bean id="viewResolver"
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>

  <!-- Access resource bundles with the specified basename -->
  <bean id="messageSource"

Spring configuration often involves writing bean definitions, such as viewResolver and messageSource shown above. A bean is simply an object which is managed by the Spring IoC container. More on this later.

Application Structure

Before we analyze the world-servlet.xml configuration file in more detail, let's take a look at the overall structure of the application, now that all the files are in place. This will help us to make sense of some of the entries in the configuration file.

Application structure for Getting Started with Spring

Tip - the Quick Start section, which follows, shows all these files in full.

Controller Class Detection

<context:component-scan> names a package to be scanned for classes with the @Controller annotation. Subpackages are automatically scanned too. The DispatcherServlet will delegate incoming web requests to the appropriate controller, based on the @RequestMapping annotations defined on these classes.

Include multiple entries if more than one package is to be scanned.

  <!-- Auto-detect controllers in these packages -->
  <context:component-scan base-package=""/>
  <context:component-scan base-package=""/>



View Resolver

The viewResolver bean uses Spring's InternalResourceViewResolver class to determine how logical view names are mapped to physical files. In this case, the file name will be made up of:
    /WEB-INF/jsp/ + view_name + .jsp
For example, a view name of countryList will result in the /WEB-INF/jsp/countryList.jsp file being executed.

Tip - this configuration allows URLs which end in .html to be mapped to JSP files. This way, the underlying web technology is not revealed to the user.

Resource Bundles

The messageSource bean uses Spring's ReloadableResourceBundleMessageSource class to locate resource bundle files. This particular configuration item will pick up any files which begin with 'messages' in the /WEB-INF directory.