找不到带URI的HTTP请求的映射?

时间:2012-02-08 11:03:36

标签: spring-mvc web.xml dispatcher

您好我有以下代码: 控制器:

@Controller

public class HelloWorldController {

    @RequestMapping("/hello")
        public ModelAndView HelloWorld() {
            String message = "My First SpringMVC Program ";
            return new ModelAndView("hello","message",message);
        }

的web.xml

<servlet>
            <!-- load on startup is used to determine the order of initializing the servlet when the application
            server starts up. The lower the number, earlier it starts -->
            <servlet-name>spring</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

弹簧servlet.xml中

<context:component-scan 
base-package="org.example.controller"/>

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

当我运行此代码时,我收到以下警告 “警告:在DispatcherServlet中找不到名为”spring“的带有URI [/SpringDemo/Hello.html]的HTTP请求的映射”。我做错了什么?

1 个答案:

答案 0 :(得分:1)

您的web.xml表示所有带有网址格式*.html的请求都会转发给Spring。您的@RequestMapping仅在/hello上过滤,但到达Spring的请求网址为/hello.html。你缺少的是.html。您的@RequestMapping应为/hello.html

在您的请求通过您的控制器后,您将转发到名为hello的视图,而spring-servlet.xml中的配置会将此解析为hello.jsp中的WEB-INF/jsp,因此请你也确定也有。

快乐的编码!