【问题标题】:Spring MVC4 No mapping found for HTTP request with URI Using mavenSpring MVC4没有为带有URI的HTTP请求找到映射使用maven
【发布时间】:2015-10-12 14:44:43
【问题描述】:

我正在使用 maven 创建一个 Spring4 MVC 应用程序 将“ma​​ven-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>

我的控制器类如下

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'

我在这里访问过更棘手的问题,但没有一个能解决我的问题。

【问题讨论】:

    标签: maven spring-mvc spring-4


    【解决方案1】:

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

    【讨论】:

    • 是的,它对我有用,现在我担心的是为什么 maven 没有创建该文件夹结构,我必须手动创建它。
    • 只需将您的 J2SE 环境更改为 Java - 7 或更高版本
    【解决方案2】:

    改变你的上下文:组件扫描基础包路径是这样的,你不需要星号:

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

    【讨论】:

    • 我已经按照您的说法尝试删除星号,但仍然有同样的错误。
    • 只需将您的 J2SE 环境更改为 Java - 7 或更高版本
    猜你喜欢
    • 2011-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多