【问题标题】:org.springframework.web.servlet.PageNotFound noHandlerFound No mapping found for HTTP requestorg.springframework.web.servlet.PageNotFound noHandlerFound 找不到 HTTP 请求的映射
【发布时间】:2020-02-04 00:33:18
【问题描述】:

我被困在这一点上,无法找到确切的原因。但是,我发现了许多类似的问题,但我的问题似乎仍然没有得到解决。我在 sts 4 中运行它并添加了一个 tomcat 服务器 9.0在本地运行它。我发现的最接近的问题是(org.springframework.web.servlet.PageNotFound noHandlerFound No mapping found for HTTP request with URI),但它似乎对我不起作用。下面是我的代码。

web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="3.0">
  <display-name>My Demo App</display-name>
  <servlet>
    <servlet-name>MyDemoApp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/myDemoApp-servletConfig.xml</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>MyDemoApp</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>
</web-app>

***myDemoApp-servletConfig.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
 <mvc:annotation-driven/>

 <context:component-scan base-package="com.demo.controllers.*"/>
 <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix ="WEB-INF/jsp/" p:suffix =".jsp"/>

</beans>


Controller



package com.demo.controllers

import org.springframework.ui.Model;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MyDemoController {

    private String[] quotes= {"To be or not to be - Shakespeare",
                              "Spring in nature's way of syaing Let's Party",
                              "The time is always right to do what is right"}


    @RequestMapping(value="/getQuote.html")
    public String getRandomQuote(Model model) {



        int rand=new Random().nextInt(quotes.length);
        String randomQuote=quotes[rand];
        model.addAttribute("randomQuote",randomQuote);
        return "quote";
    }

}


  [1]: https://i.stack.imgur.com/Z5SN3.jpg

【问题讨论】:

  • 你的WEB-INF/jsp/文件夹中有quotes.jsp吗?
  • @SujayMohan 是的,我在提到的文件夹中有quotes.jsp。
  • 您要访问的网址是什么?也可以试试&lt;url-pattern&gt;/&lt;/url-pattern&gt;
  • @SujayMohan localhost:8080/springMVCDemo/getQuote.html :这是我要调用的网址。
  • 试试 /*.html

标签: java spring spring-mvc servlets web.xml


【解决方案1】:

那么,实际上我并没有在 Eclipse 中工作,但是你有两个 src 文件夹吗?如果是这样,您需要将所有类和资源保存在一个 src 文件夹下,如下所示:

-src 
   -main  
      -java (Your controllers)
          -other packages under java folder
      -resources (spring configuration file)
      -webapp (jsp, web.xml etc.)

除此之外,我猜线路不正确

<context:component-scan base-package="com.demo.controllers.*"/>

也许它在早期版本的 spring 中是允许的。但是如果你会看到官方文档,我没有看到使用带有'*'符号的版本。我试图在本地使用它,它对我不起作用。所以使用com.demo.controllers

【讨论】:

  • 我仍然遇到同样的错误。之前我只保留了“com.demo.controllers”,但是在查看stackoverflow上的一些问题时,我发现“*”最后也可以使用.
  • 你能附上你更新的项目结构的屏幕吗?
  • 请找到网址:i.stack.imgur.com/zwByI.jpg.I 我正在使用相同的 spring 工具套件。
  • 您只有一个 src 文件夹吗?在屏幕上看起来你有两个
  • 我尝试删除 src/main/java 文件夹并将整个包移动到第二个 src/main 文件夹,但问题仍然存在。
猜你喜欢
  • 2015-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多