Spring MVC4 使用 maven 未找到具有 URI 的 HTTP 请求的映射

Spring MVC4 No mapping found for HTTP request with URI Using maven

我正在使用 maven 创建一个 Spring4 MVC 应用程序 将“maven-archetype-webapp”作为 artifactId 和 添加依赖项 "Spring-core"、"spring-web"、"Spring-mvc" 下面是 "web.xml" & "HelloWeb-servlet.xml"

web.xml

<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
  <servlet-name>HelloWeb</servlet-name>
  <servlet-class>
     org.springframework.web.servlet.DispatcherServlet
  </servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>HelloWeb</servlet-name>
  <url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>

HelloWeb-servlet.xml

 <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-4.0.xsd
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-4.0.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

<context:component-scan base-package="com.zap.pm.*" />
<mvc:annotation-driven />
<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

我的控制器class如下所示

package com.zap.pm;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController{

@RequestMapping( value="/test")
public String printHello() {
    ModelAndView model=new ModelAndView();
  model.addAttribute("message", "Hello Spring MVC Framework!");
  return "hello";
  }
}

我已将 hello.jsp 放在“/WEB-INF/jsp/”中 但是在访问请求“http://localhost:8080/webappspring/test.html”时 它给出了以下错误

Oct 12, 2015 7:10:13 PM     org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/webappspring/test.html] in DispatcherServlet with name 'HelloWeb'

我已经在此处访问了更复杂的问题,但 none 正在解决我的问题。

将您的 context:component-scan base-package 路径更改为这个,您不需要星号:

<context:component-scan base-package="com.zap.pm" />

请检查您的应用程序的文件夹结构,我遇到了同样的问题,我的控制器在 src/main/resources 文件夹而不是 src/main/java.