当前位置: 首页 > 知识库问答 >
问题:

Spring MVC-在名称为DefaultServlet的DispatcherServlet中找不到与URI[/webstore/]的HTTP请求的映射

刘弘济
2023-03-14

我在阅读《Spring MVC初学者指南》一书的同时,遇到了一些似乎是请求映射问题的问题。

我在其他类似的Stack Overflow问题中看到并尝试了以下解决方案:

  • 将“url模式”从/更改为/*

我有以下代码:

目录结构:

家庭控制器。JAVA

package webstore;

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

@Controller
public class HomeController {

    @RequestMapping(value="/")
    public String welcome(Model model) {
        model.addAttribute("greeting", "Welcome to Web Store!");
        model.addAttribute("tagline", "The one and only amazing webstore!");
        return "welcome";
    }

}

欢迎jsp

<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core"%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
        <title>Welcome</title>
    </head>
    <body>
        <section>
            <div class="jumbotron">
                <div class="container">
                    <h1>${greeting} </h1>
                    <p>${tagline} </p>
                </div>
            </div>
        </section>
    </body>
</html>

DefaultServlet。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-4.0.xsd
        http://www.springframework.org/schema/mvc   
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.packt.webstore" />

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

web.xml

<web-app version =" 3.0" 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_3_0.xsd">


    <servlet>
        <servlet-name>DefaultServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    </servlet>
    <servlet-mapping> 
        <servlet-name>DefaultServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


</web-app>

以下是我在IDE(Spring工具套件)中启动服务器时的整个服务器日志:

Feb 12, 2015 12:32:07 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:webstore' did not find a matching property.
Feb 12, 2015 12:32:07 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/7.0.59
Feb 12, 2015 12:32:07 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Jan 28 2015 15:51:10 UTC
Feb 12, 2015 12:32:07 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         7.0.59.0
Feb 12, 2015 12:32:07 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Mac OS X
Feb 12, 2015 12:32:07 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            10.9.2
Feb 12, 2015 12:32:07 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          x86_64
Feb 12, 2015 12:32:07 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             /Library/Java/JavaVirtualMachines/jdk1.7.0_60.jdk/Contents/Home/jre
Feb 12, 2015 12:32:07 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.7.0_60-b19
Feb 12, 2015 12:32:07 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Feb 12, 2015 12:32:07 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         /Users/richiethomas/Documents/workspace-sts-3.6.3.SR1/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Feb 12, 2015 12:32:07 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         /Users/richiethomas/Downloads/apache-tomcat-7.0.59
Feb 12, 2015 12:32:07 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/Users/richiethomas/Documents/workspace-sts-3.6.3.SR1/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Feb 12, 2015 12:32:07 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/Users/richiethomas/Downloads/apache-tomcat-7.0.59
Feb 12, 2015 12:32:07 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=/Users/richiethomas/Documents/workspace-sts-3.6.3.SR1/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps
Feb 12, 2015 12:32:07 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=/Users/richiethomas/Downloads/apache-tomcat-7.0.59/endorsed
Feb 12, 2015 12:32:07 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
Feb 12, 2015 12:32:07 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /Users/richiethomas/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
Feb 12, 2015 12:32:07 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Feb 12, 2015 12:32:07 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Feb 12, 2015 12:32:07 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 536 ms
Feb 12, 2015 12:32:07 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Feb 12, 2015 12:32:07 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.59
Feb 12, 2015 12:32:07 AM org.apache.catalina.deploy.WebXml setVersion
WARNING: Unknown version string [ 3.0]. Default version will be used.
Feb 12, 2015 12:32:08 AM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Feb 12, 2015 12:32:08 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Feb 12, 2015 12:32:08 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Feb 12, 2015 12:32:08 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1010 ms

共有3个答案

傅元章
2023-03-14

从控制器方法中的@RequestMapping(value=“/”)中删除“value=”。

寇靖
2023-03-14

在网上。尝试改变

<servlet>
    <servlet-name>DefaultServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

</servlet>

<servlet>
    <servlet-name>DefaultServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/DefaultServlet-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
欧博简
2023-03-14

您的问题似乎是组件扫描,您扫描了错误的包。您的控制器位于webstore包中,因此更改您的servlet配置:

 <context:component-scan base-package="webstore" />

或者将控制器移动到com.packt.webstore包。

当你的应用被部署时,你应该在日志中看到注册了哪些url映射,然后你就会知道这个修复程序是否有效。

 类似资料: