【问题标题】:No mapping found for HTTP request with URI [/Demo_SpringMVC/add] in DispatcherServlet [duplicate]在 DispatcherServlet 中找不到带有 URI [/Demo_SpringMVC/add] 的 HTTP 请求的映射 [重复]
【发布时间】:2020-08-29 22:47:20
【问题描述】:

只是尝试使用 maven 创建一个简单的 Spring MVC 应用程序,但出现此错误(未找到带有 URL 的请求的映射)。尝试了之前为此类错误提供的所有可能解决方案,但没有得到正确的结果。任何帮助都会非常感谢。在此先感谢您。

稍后我将尝试实现视图解析器部分,这里只是尝试使用spring mvc在index.jsp页面上单击提交按钮后显示一个jsp页面。

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.subu</groupId>
<artifactId>Demo_SpringMVC</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Demo_SpringMVC Maven Webapp</name>
<url>http://maven.apache.org</url>

<dependencies>
 <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.1</version>
   <scope>test</scope>
 </dependency>
 
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-webmvc</artifactId>
     <version>4.1.8.RELEASE</version>
 </dependency>
 
 <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <version>5.1.36</version>
 </dependency>
 
  <dependency>
     <groupId>javax.servlet</groupId>
     <artifactId>jstl</artifactId>           
     <version>1.2</version>
  </dependency>
  
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context</artifactId>
 <version>4.1.8.RELEASE</version>
</dependency>
</dependencies>
<build>
 <finalName>Demo_SpringMVC</finalName>
</build>
</project>


index.jsp

<html>
<body>


<form action="add">
    <input type="text" name="t1"><br>
    <input type="text" name="t2"><br>
    <input type="submit">
</form>
</body>
</html>

web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  
  <servlet>
    <servlet-name>subu</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>subu</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
</web-app>

Dispatcher-Servlet 命名为 subu-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:ctx="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-2.5.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
    
    
    <ctx:annotation-config></ctx:annotation-config>
    <ctx:component-scan base-package="com.subu.*"></ctx:component-scan>
</beans>

名为 AddController.java 的控制器类:

package com.subu;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class AddController {
    
    @RequestMapping("/add")
    public String add()
    {
        return "display.jsp";
    }

}


项目目录是这样的:

【问题讨论】:

  • 稍后查看解析器部分是什么意思? Jsp 是一个视图,Spring 需要该视图解析器来识别要显示的视图。如果您没有视图解析器,您应该会收到此错误。
  • 这能回答你的问题吗? Problems with rendering JSP in spring boot
  • 关于视图解析器部分,dispatcher servlet 需要知道要显示哪个视图页面及其格式。这里我只在控制器的方法中提供视图页面名称和格式。不要认为这是一个问题。
  • 即使我没有提到 return "display.jsp" 并且如果我提到 system.out.println("here"),那么我也会遇到同样的错误,而不会在控制台中获得任何文本
  • 这很正常,因为你的控制器还没有被调用,由于那个404,请求无法访问你的控制器

标签: java spring maven spring-mvc


【解决方案1】:

不确定它是否会完全解决您的问题,但我看到了一些配置问题。

1. Spring 正在搜索 URL /Demo_SpringMVC/add 的映射。其从pom.xmlfinalName中挑选基本URL

执行以下任一操作

<servlet-mapping>
    <servlet-name>subu</servlet-name>
    <url-pattern>/Demo_SpringMVC/*</url-pattern>
  </servlet-mapping>

或像这样在控制器级别映射

@Controller
@RequestMapping("/Demo_SpringMVC")
public class AddController {
    
    @RequestMapping("/add")
    public String add()
    {
        return "display.jsp";
    }

}

以上操作至少会触发add() 方法。通过添加 sysout 进行验证。

2. 我没有看到任何与 View Resolver 相关的配置。最好添加一个,如下所示。另外,在WEB-INF 文件夹中添加JSP。

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/"/>
    <property name="suffix" value=".jsp"/>
</bean>

【讨论】:

    【解决方案2】:

    根据您的问题,错误如下,

    未找到包含 URI [/Demo_SpringMVC/add] 的 HTTP 请求的映射 名为“subu”的 DispatcherServlet

    出现此错误的原因是,未在您的 subu-servlet.xml 文件中配置 InternalResourceViewResolver。它用于将 URI 映射到实际的 URI。欲了解更多信息,spring documentationA Guide to the ViewResolver in Spring MVC

    修改subu-servlet.xml文件如下,

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:ctx="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-2.5.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
        
        
        <ctx:annotation-config></ctx:annotation-config>
        <ctx:component-scan base-package="com.subu.*"></ctx:component-scan>
    
        <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name = "prefix" value = "/WEB-INF/jsp/" />
            <property name = "suffix" value = ".jsp" />
        </bean>
    </beans>
    

    最佳实践

    在“WEB-INF”文件夹下创建一个名为“jsp”的文件夹,并将所有.jsp文件移动到“jsp”文件夹中。 (以上答案基于此提示)

    【讨论】:

      【解决方案3】:

      好的,我明白了。由于调度程序 servlet 中的这一行,我收到了无映射错误:

       <ctx:component-scan base-package="com.subu.*"></ctx:component-scan>
      

      从基础包中删除 * 后能够获得预期的正确输出。

       <ctx:component-scan base-package="com.subu"></ctx:component-scan>
      

      关于视图解析器部分,我还没有实现它并且仍然从 display.jsp 页面获得正确的输出。我认为这里不需要视图解析器,因为我自己将 display.jsp 页面提供给控制器。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-17
        • 2016-06-11
        • 2015-06-03
        • 2021-08-01
        • 1970-01-01
        • 2013-09-12
        • 2016-04-01
        相关资源
        最近更新 更多