0

我正在尝试创建一个简单的 spring mvc 应用程序进行练习,但我不断收到此错误:

在名为“mvc-dispatcher”的 DispatcherServlet 中找不到带有 URI 的 HTTP 请求的映射,我从 Tomcat 收到 404 错误。

这是我的 mvc-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<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="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" >

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

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

<mvc:annotation-driven />

<context:component-scan base-package="controllers" />

<bean
    id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver" >

    <property
        name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />

    <property
        name="suffix"
        value=".jsp" />
</bean>

</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd" id="WebApp_ID" version="3.0">  
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
   <listener-class>
      org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener>

</web-app>

这是我的控制器

package controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/welcome.do")
public class HelloWorldController{

@RequestMapping(method=RequestMethod.GET)
public ModelAndView helloWorld(){
    System.out.println("**Hit controller**");
    ModelAndView model = new ModelAndView("spring");
    model.addObject("msg", "hello world");
    return model;
}
}

在调度程序 servlet 中使用 bean 时,我可以让控制器工作,但由于某种原因,我无法使用注释获得正确的处理程序映射。我是否在猜测我的 mvc-dispatcher-servlet.xml 文件中有一些东西把事情搞砸了,任何帮助都会很棒。

4

3 回答 3

0

您的“mvc-dispatcher-servlet.xml”看起来正确,它对我有用。确保您使用正确的网址。你可以看看这个简单的例子Spring MVC Hello World Annotation Example

于 2013-01-28T09:03:17.293 回答
0

首先使用以下内容交叉检查您的包名称

<context:component-scan base-package="controllers" />

可能有可能不正确,base-package 表示完整的包路径,如“org.self.etc”

如果正确,则将此行放在上面<mvc:annotation-driven />

于 2014-04-26T09:46:17.023 回答
0

为父弹簧上下文设置弹簧配置。

MVC 仅适用于 ServletDispatcher:

<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd" id="WebApp_ID" version="3.0">  
<servlet>

<servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>

<servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

<listener>
   <listener-class>
      org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener>

</web-app>
于 2020-10-05T21:36:57.077 回答