【问题标题】:WARNING: No mapping found for HTTP request with URI [/Test/] in DispatcherServlet with name 'dispatcher'警告:在名称为“dispatcher”的 DispatcherServlet 中找不到带有 URI [/Test/] 的 HTTP 请求的映射
【发布时间】:2019-03-19 16:17:54
【问题描述】:

我正在使用spring框架进行编程,实际上我创建了一个简单的网络应用程序,但有时我会得到答案,有时会出错

spring-mvc-demo-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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- Step 3: Add support for component scanning -->
    <context:component-scan base-package="com.luv2code.springdemo/home" />

    <!-- Step 4: Add support for conversion, formatting and validation support -->
    <mvc:annotation-driven/>

    <!-- Step 5: Define Spring MVC view resolver -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>


</beans>

web.xml

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" 版本="3.1">

<display-name>Test</display-name>

<!-- Spring MVC Configs -->

<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

HomeController.java

package com.luv2code.springdemo.mvc;

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

@Controller
public class HomeController {

    @RequestMapping("/home")
    public String showPage() {
        return "main-menu";
    }
}

主菜单.jsp

<!DOCTYPE>
<html>

<body>

<h2>Spring MVC Demo - Home Page</h2>

</body>

</html>

【问题讨论】:

  • 同一个线程我没有得到答案。
  • 您会发布异常根跟踪还是应用程序日志?
  • 我尝试了不同的项目名称,但答案总是。警告 [http-nio-8084-exec-86] org.springframework.web.servlet.DispatcherServlet.noHandlerFound 在 DispatcherServlet 中找不到带有 URI [/spring-mvc-demo/] 的 HTTP 请求的映射,名称为“dispatcher”

标签: java spring spring-mvc


【解决方案1】:

我已经尝试了相同的代码,你所要做的就是

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

即包名

http://localhost:8081/Test/home 可以正常工作。

如果您需要,请查看以下链接 HTTP Status 404 - on Eclipse with Tomcat

【讨论】:

  • 还没有。首先我尝试了 Eclipse,现在我尝试了 net beans,但我没有得到答案
  • 上述问题的主要目的是我需要知道为什么有时工作有时不工作
【解决方案2】:

我遇到了同样的错误,解决方法是简单地查看spring-mvc-demo-servlet.xml 文件中的组件扫描标记。您使用的是讲师的 com.luv2code,但您的 package name 不同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多