【问题标题】:Spring MVC - noHandlerFound / PageNotFoundSpring MVC - noHandlerFound / PageNotFound
【发布时间】:2017-05-05 11:38:17
【问题描述】:

我发现很多类似的问题被问到我遇到的问题,并尝试了很多解决方案,但似乎都没有奏效。

最奇怪的是我的一位同事可以运行程序所以代码不是问题。

我得到的错误是当我运行应用程序时,我无法访问welcomepage.htmlhellopage.html

十二月。 2016 年 2 月 20 日上午 9:42:37 org.springframework.web.servlet.DispatcherServlet noHandlerFound 声明:未找到带有 URI 的 HTTP 请求的映射 [/SpringMVC/hellopage.html] 在 DispatcherServlet 中,名称为“spring”

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</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: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="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">

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

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

</beans>

HelloWorldController.java

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

@Controller  
public class HelloWorldController {  

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

        String message = "HELLO SPRING MVC";  
        return new ModelAndView("hellopage", "message", message);  
    }  

}

WelcomeWorldController.java

package com.javatpoint;

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

@Controller
public class WelcomeWorldController {

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

        String message = "WELCOME SPRING MVC";
        return new ModelAndView("welcomepage", "message", message);
    }

}

index.jsp

<html>
<body>
<a href="hellopage.html">click</a>
|
<a href="welcomepage.html">click</a>
</body>
</html>

这是我使用 tomcat 7.0.73 运行的 maven 项目

有项目目录的结构

https://i.stack.imgur.com/PmNBm.png

【问题讨论】:

  • 检查您的上下文路径。在 Eclipse 中右键单击您的服务器并转到模块查看项目的路径。是否是 springMVC
  • 错误很明显,* No mapping found [/SpringMVC/hellopage.html] * .您发布的控制器映射在 /welcome 和 /hello 中。 *.html 中没有任何内容.........

标签: java spring maven jsp spring-mvc


【解决方案1】:

当您使用 @RequestMapping("/hello") 配置控制器 (HelloWorldController) 请求映射时,您的 URL 应该是 /SpringMVC/hello.html(而不是 /SpringMVC/hellopage.html)。 (WelcomeWorldController) 请求映射也一样。

【讨论】:

    【解决方案2】:

    正如您所说,代码在您朋友的系统上运行时没有问题,我假设配置存在问题。

    请验证项目名称在所有配置文件和 pom.xml 中是否正确

    【讨论】:

      猜你喜欢
      • 2016-02-13
      • 1970-01-01
      • 2013-10-09
      • 2020-11-21
      • 2011-04-22
      • 2016-02-09
      • 1970-01-01
      • 2015-04-14
      • 2015-10-21
      相关资源
      最近更新 更多