我在 Eclipse 中制作了一个简单的 Spring MVC 应用程序。它开始给注释带来问题

I made a simple Spring MVC application in Eclipse. It start to give problems with annotations

当我访问 First_Spring_MVC/welcome 时出现以下错误:

org.springframework.web.servlet.PageNotFound noHandlerFound

WARNING: No mapping found for HTTP request with URI [/First_Spring_MVC/welcome] in DispatcherServlet with name 'spring-dispatcher'

这是我的文件夹结构:

这是我的 web.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>First_Spring_MVC</display-name>
  <servlet>
    <servlet-name>spring-dispatcher</servlet-name>
    <servlet-class>
    org.springframework.web.servlet.DispatcherServlet
  </servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

这里是 spring-调度程序-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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">

<context:component-scan base-package="com.webapp.helloController" /> 

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

这是我用于控制器 class 的 java 文件。

@Controller
public class HelloController {

    @RequestMapping("/welcome")
    public ModelAndView helloWorld(){

        ModelAndView model = new ModelAndView("HelloPage");
        model.addObject("welcomeMessage", "hello world");

        return model;
    }
}

您正在扫描错误的包裹。变化:

<context:component-scan base-package="com.webapp.helloController" />

<context:component-scan base-package="com.app.helloController" /> 

还要确保你打的是正确的 url,在你的情况下:

http://localhost:{your_port_num}/First_Spring_MVC/welcome