0

我试图用 Spring 4.0.2 创建一个非常简单的控制器。如果我使用 3.1.2.RELEASE 版本,它可以正常工作。但是,一旦我将 maven 依赖项升级到 4.0.2.RELEASE。它停止工作。

这是我的 web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
id="WebApp_ID" version="2.5">
<display-name>my-app</display-name>
<servlet>
    <servlet-name>springDispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springDispatcher</servlet-name>
    <url-pattern>*.json</url-pattern>
</servlet-mapping>
</web-app>

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

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

<context:component-scan base-package="com.tsun.sample"/>
<bean  
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" 
/>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" 
/>
</beans>

这是我的控制器

package com.tsun.sample;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class TestController {
    @RequestMapping(value="/test.json", method=RequestMethod.GET)
    public String simpleMethod(ModelMap model) {
        return "hello";
   }
}

这是我在 WEB-INF/jsp 中的 hello.jsp

HELLO WORLD!

当我尝试

http://localhost:8080/my-app/test.json 

有错误 2014-03-14 14:38:34,417 WARN [org.springframework.web.servlet.PageNotFound] (http-0.0.0.0-8080-1) No mapping found for HTTP request with URI [/my-app/ test.json] 在 DispatcherServlet 中,名称为“springDispatcher”

我错过了什么吗?它适用于 Spring 版本 3.1.2 欢迎任何反馈!

谢谢

大卫

4

2 回答 2

0

您可以先尝试迁移到 Spring 3.2 吗?使用官方迁移指南到 3.2应该会对您有所帮助。同样在您的情况下,我认为注释bean可以替换为:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven/>
    <!-- your other beans -->

</beans>

完成后,您可以依赖Spring 4 迁移指南

不用担心,这两个指南都非常轻量级,将两个任务分开应该会有很大帮助。注意:查看Spring 4 依赖项更新以了解最低要求。Jboss 5.2 似乎没有满足某些要求。事情可能会奏效,但无法获得官方支持。

于 2014-03-15T13:06:15.313 回答
0

我怀疑这与由于 Spring 4 删除了对 JBoss 5.2 使用的虚拟文件系统 (VFS) 版本的支持而导致组件扫描无法正确找到资源有关。

Antoine Rey 已经讨论过这个问题(用法语),并开发了一个可以从 Github 获得的修复程序。

于 2014-06-02T10:45:11.143 回答