我试图用 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 欢迎任何反馈!
谢谢
大卫