2. Spring Configuration

The context:component-scan registers the PersonController, which is defined in the org.springbyexample.web.servlet.mvc package. The first import configures the JPA configuration for managing a person and the second configures Spring MVC.

/WEB-INF/simple-form-servlet.xml
                
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="org.springbyexample.web.servlet.mvc" />

    <!-- Imports the configurations of the different infrastructure systems of the application -->
    <import resource="data-access-context.xml" />
    <import resource="webmvc-context.xml" />

</beans>
                
            

This standard Spring MVC configuration file creates handlers, configures Tiles, and also internationalization. The classnameControllerMappings bean enables convention based mappings for reduced configuration. It is configured to be case sensitive, so a controller called StudentPersonController would map to the URL '/studentPerson'. Although it defaults to case insensitive and would then map to the URL '/studentperson'. The default handler is set with the UrlFilenameViewController, which will handle any requests not handled by a convention based controller. The tilesConfigurer bean configures tiles and dynamicTilesViewResolver takes the url of a request and uses it for the body in a Tiles template. The last three beans configure locale and messsage resource handling.

/WEB-INF/simple-form-servlet.xml
                
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- Enables /[resource]/[action] to [Resource]Controller class mapping -->
    <bean id="classnameControllerMappings" 
          class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"
          p:interceptors-ref="localeChangeInterceptor" 
          p:caseSensitive="true">
        <property name="defaultHandler">
            <bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
        </property>
    </bean>

    <!-- Enables annotated POJO @Controllers -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

    <!-- Enables plain Controllers -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

    <bean id="tilesConfigurer"
          class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles-defs/templates.xml</value>
            </list>
        </property>
    </bean>
 
    <bean id="tilesViewResolver"
          class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springbyexample.web.servlet.view.tiles2.DynamicTilesView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <value>messages</value>
        </property>
    </bean>
    
    <!-- Declare the Interceptor -->
    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="locale"/>
    </bean>
    
    <!-- Declare the Resolver -->
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />

</beans>