<?xml version="1.0" encoding="UTF-8"?>

<!--
========================================================================================================================
main-servlet.xml

Copyright (c) 2013 Manning Publications Co.

Joshua White

Book: http://manning.com/wheeler/
Blog: http://springinpractice.com/
Code: https://github.com/springinpractice
========================================================================================================================
-->
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
    
    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven>
        <argument-resolvers>                        
            <!-- Resolves the Device @Controller method argument -->
            <beans:bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
            
            <!-- Resolves the SitePreference @Controller method argument -->
            <beans:bean class="org.springframework.mobile.device.site.SitePreferenceWebArgumentResolver" />                                         
        </argument-resolvers>
    </annotation-driven>
    
    <!-- 
        JW: Here we are registering custom HandlerInterceptors for Spring Mobile that
        will be applied to all HandlerMapping beans.
     -->
    <interceptors>
        <!-- On pre-handle, resolve the device that originated the web request -->
        <beans:bean class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />
        
        <!-- On pre-handle, manage the user's site preferences (declare after DeviceResolverHandlerInterceptor) -->
        <beans:bean class="org.springframework.mobile.device.site.SitePreferenceHandlerInterceptor" />      
    </interceptors>

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />
                
    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean id="viewResolver" class="com.springinpractice.ch03.view.SitePreferenceDelegatingViewResolver">
        <beans:property name="normalViewResolvers">
            <beans:list>
                <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <beans:property name="order" value="1" />
                    <beans:property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
                    <beans:property name="prefix" value="/WEB-INF/jsp/normal/" />
                    <beans:property name="suffix" value=".jsp" />
                </beans:bean>               
            </beans:list>
        </beans:property>       
        <beans:property name="mobileViewResolvers">
            <beans:list>
                <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <beans:property name="order" value="1" />
                    <beans:property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
                    <beans:property name="prefix" value="/WEB-INF/jsp/mobile/" />
                    <beans:property name="suffix" value=".jsp" />
                </beans:bean>               
            </beans:list>
        </beans:property>       
    </beans:bean>
        
    <context:component-scan base-package="com.springinpractice.ch03" />
            
</beans:beans>
