0

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

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

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

有什么我想念的想法吗?

4

2 回答 2

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>

In your case I think that's all you need.

However I've had problems with some Java application servers when they automatically handle *.js URLs with their internal static file handler instead of going to Java code. If you have that problem, change your servlet-mapping in your web.xml to ensure that Spring handles those URLs instead of the inbuilt file hander. For example:

<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>
于 2012-11-22T02:08:14.567 回答
0

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 />
于 2012-11-27T20:43:14.237 回答