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

时间:2015-02-12 08:13:50

标签: java spring spring-mvc

我正在阅读“Spring MVC初学者指南”一书,我遇到了似乎是请求映射问题。

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

  • 将“url-pattern”从/更改为/ *
  • 将RequestMapping注释的url-pattern和参数更改为“/ welcome”
  • <mvc:default-servlet-handler/>添加到DefaultServlet-servlet.xml
  • 将网址格式更改为“/ WEB-INF / jsp / *”

我有以下代码:

目录结构:

enter image description here

HomeController.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";
    }

}

的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-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-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 Tool Suite)中启动服务器时,这是整个服务器日志:

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 个答案:

答案 0 :(得分:1)

您的问题似乎与组件扫描有关, 你正在扫错包裹。您的控制器位于webstore包中,因此请更改您的servlet配置:

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

或将控制器移至com.packt.webstore包。

当您的应用程序被部署时,您应该在日志中看到已注册的url映射,然后您将知道此修复是否有效。

答案 1 :(得分:1)

web.xml 尝试更改

<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>

答案 2 :(得分:0)

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