如何使用Spring MVC正确映射DWR 3?

时间:2012-11-21 17:57:37

标签: java spring spring-mvc dwr url-mapping

我正在尝试将Spring 3.1.1.RELEASE与DWR 3(v 3.0.0-rc2)集成。我正在使用Spring MVC,但无法使设置正常工作。 Spring无法映射/dwr/engine.js(或其他任何正确的/ dwr。我的应用程序日志文件中出现此错误...

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'

这是我的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>

这是我的spring应用程序上下文文件(DWR配置在最后):

<?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>

我缺少什么想法?

2 个答案:

答案 0 :(得分:0)

看起来你拥有所需的所有部件,但你需要加入它们。我看不到从Spring MVC到DWR控制器的任何映射,这就是Spring MVC无法识别这些URL的原因。

将以下内容添加到Spring应用程序上下文配置文件中应该这样做:

<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

<bean id="viewMappings" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="alwaysUseFullPath" value="true" />
    <property name="urlMap">
         <map>
              <entry key="/dwr/**/*" value-ref="dwrController" />
         </map>
    </property>
</bean>

在你的情况下,我认为这就是你所需要的。

但是当我们使用内部静态文件处理程序自动处理* .js URL而不是使用Java代码时,我遇到了一些Java应用程序服务器的问题。如果您遇到此问题,请更改web.xml中的servlet-mapping以确保Spring处理这些URL而不是内置文件处理程序。例如:

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

答案 1 :(得分:0)

我能够使用这个web.xml配置来解决我的问题...

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

和这个Spring MVC配置......

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