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.

DispatcherServlet

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

  <!-- A servlet that dispatches requests to registered controllers -->
  <servlet>
    <servlet-name>world</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <!--  Send all .html files to the Spring dispatcher servlet -->
  <servlet-mapping>
    <servlet-name>world</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>

  <!-- Define the web application entry point -->
  <welcome-file-list>
    <welcome-file>countryList.html</welcome-file>
  </welcome-file-list>

</web-app>

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:
<beans>

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

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

  <!-- Access resource bundles with the specified basename -->
  <bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
    p:basename="/WEB-INF/messages"/>

</beans>
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
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.
<beans>

  <!-- Auto-detect controllers in these packages -->
  <context:component-scan base-package="levelup.world.web"/>
  <context:component-scan base-package="levelup.world.webplus"/>

  ..

</beans>

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
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.
»