Search code examples
javaspringspring-mvcdwrurl-mapping

How do I map DWR 3 properly using Spring MVC?


I'm trying to integrate Spring 3.1.1.RELEASE with DWR 3 (v 3.0.0-rc2). I'm using Spring MVC but am not able to get the setup to work properly. Spring isn't able to map /dwr/engine.js (or anything else with /dwr properly. I get this error in my app log files …

11:43:31,237 WARN  [org.springframework.web.servlet.PageNotFound] (http--127.0.0.1-8080-4) No mapping found for HTTP request with URI [/myapp-1.0-SNAPSHOT/dwr/engine.js] in DispatcherServlet with name 'dispatcher'

Here is my web.xml …

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                            http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

    <display-name>SB Admin</display-name>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/jboss-as-spring-mvc-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/META-INF/spring/applicationContext-myapp.xml</param-value>
    </context-param>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

And here is my spring application context file (the DWR configuration is at the end):

<?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"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
    xsi:schemaLocation="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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">

    <context:component-scan base-package="org.myco.subco" />

    <!-- Enable annotation driven controllers, validation etc... -->
    <mvc:annotation-driven />

    <!-- the mvc resources tag does the magic -->
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <util:properties id="applicationProperties" location="classpath:application.properties" />

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
    </bean>

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:jboss/datasources/MySqlDS" />
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
        <property name="persistenceUnitName" value="myappunit" />
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="sharedEntityManager"
        class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <dwr:annotation-scan base-package="org.myco.subco" scanDataTransferObject="true" scanRemoteProxy="true" />
    <dwr:url-mapping /> 
    <dwr:controller id="dwrController" debug="true" />
    <dwr:configuration />

</beans>

Any ideas what I'm missing?


Solution

  • I was able to solve my problem using this web.xml configuration ...

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/jboss-as-spring-mvc-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/dwr/*</url-pattern>
    </servlet-mapping>
    
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    

    and this Spring MVC config ...

    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
    
    <!-- DWR will scan all Spring managed beans containing @RemoteProxy or @RemoteMethod 
        annotations and register Creator proxies for them. This will NOT scan any 
        classes not managed by Spring. -->
    <dwr:annotation-config id="springdwr" />
    
    <!-- DWR will scan the classpath and search classes containing @RemoteProxy 
        or @RemoteMethod annotations. This will register the beans and Creator proxies 
        for these classes. -->
    <dwr:annotation-scan base-package="org.collegeboard.springboard"
        scanDataTransferObject="true" scanRemoteProxy="true" />
    
    <!-- DWR will map util.js and engine.js files to the dwrController. You 
        can then include this files as external Javascript references from your JSP -->
    <dwr:url-mapping />
    
    <!-- Defines the dwrController. During production, set the debug property 
        to false -->
    <dwr:controller id="dwrController" debug="true" />
    
    <!-- This is required if you want to configure any beans not managed by 
        Spring. Leaving it enabled doesn't do any negative effects. Here's a sample 
        config: -->
    <dwr:configuration />