java - org.springframework.web.servlet.PageNotFound noHandlerFound没有为HTTP请求找到映射

原文 标签 java spring spring-mvc servlets web.xml

我被困在这一点上,无法找到确切的原因。但是,我发现了很多类似的问题,但是我的问题似乎仍然没有解决。我在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]: /image/Z5SN3.jpg

最佳答案

因此,实际上我没有在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

关于java - org.springframework.web.servlet.PageNotFound noHandlerFound没有为HTTP请求找到映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58257253/

相关文章:

java - 如何从java应用程序订阅apache kafka中的主题?

java - 如何同时进行多个 jsoup session (Spring Boot 项目和并发)

java - SpringJUnit4ClassRunner上下文初始化之前如何运行代码?

java - 启动spring web mvc应用程序时出现异常

java - hibernate 关系映射不双向

java - JRE是否具有诊断工具?

java - 按字符串排序进入数组字母Java

java - 使用 javafx 中的单选按钮打开/关闭 slider

java - SQL 状态 [null];错误代码[0]; ORA-00900 : invalid SQL statement with jdbcTemplate

java - @Autowired 给出空指针异常