【问题标题】:Mapping issues with Spring-webmvc [duplicate]Spring-webmvc 的映射问题 [重复]
【发布时间】:2014-02-17 12:41:47
【问题描述】:

在设置 Spring 应用程序(打包为 WAR 并通过 Tomcat 托管)时,当我尝试访问“localhost:8080”时出现 404 和以下错误:

4479 [http-bio-8080-exec-1] WARN  org.springframework.web.servlet.PageNotFound  - No mapping found for HTTP request with URI [/WEB-INF/pages/index.html] in DispatcherServlet with name 'spring'

肯定有一个 /WEB-INF/pages/index.html 文件。

这是我的 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app> 

另外,这是我的 spring-servlet.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans 
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.company.app.controller" />

    <mvc:annotation-driven />

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

</beans>

还有来自我的控制器的 get 方法:

package com.company.app.controller;

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

@Controller
public class SplashController {

    @RequestMapping(value="/", method=RequestMethod.GET)
    public String index() {
        return "index";
    }

}

那么,我的配置有什么问题?

另外:我想包含 Spring 初始设置的控制台输出,也许它会提供一两个关于缺少什么的线索:

0    [localhost-startStop-1] INFO  org.springframework.web.context.ContextLoader  - Root WebApplicationContext: initialization started
92   [localhost-startStop-1] INFO  org.springframework.web.context.support.XmlWebApplicationContext  - Refreshing Root WebApplicationContext: startup date [Sun Jan 26 16:16:57 EST 2014]; root of context hierarchy
147  [localhost-startStop-1] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-servlet.xml]
776  [localhost-startStop-1] INFO  org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping  - Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.company.app.controller.SplashController.index()
1547 [localhost-startStop-1] INFO  org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping  - Root mapping to handler 'splashController'
1771 [localhost-startStop-1] INFO  org.springframework.web.context.ContextLoader  - Root WebApplicationContext: initialization completed in 1770 ms
Jan 26, 2014 4:16:59 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'spring'
1809 [localhost-startStop-1] INFO  org.springframework.web.servlet.DispatcherServlet  - FrameworkServlet 'spring': initialization started
1813 [localhost-startStop-1] INFO  org.springframework.web.context.support.XmlWebApplicationContext  - Refreshing WebApplicationContext for namespace 'spring-servlet': startup date [Sun Jan 26 16:16:59 EST 2014]; parent: Root WebApplicationContext
1817 [localhost-startStop-1] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-servlet.xml]
1947 [localhost-startStop-1] INFO  org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping  - Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.company.app.controller.SplashController.index()
2164 [localhost-startStop-1] INFO  org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping  - Root mapping to handler 'splashController'
2205 [localhost-startStop-1] INFO  org.springframework.web.servlet.DispatcherServlet  - FrameworkServlet 'spring': initialization completed in 396 ms

【问题讨论】:

  • 您在浏览器中输入了什么网址?
  • 我输入了 localhost:8080
  • 请粘贴整个 spring-servlet.xml
  • 我已经更新了 spring-servlet.xml(并且还为我的控制器类添加了包/导入)。
  • 我找不到 mvc:annotation-driven - 请检查将这个 stag 添加到 spring-servlet.xml 是否没有帮助

标签: spring spring-mvc servlets http-status-code-404


【解决方案1】:

无论出于何种原因,将我的 web.xml 的 servlet-mapping 部分更改为此解决了问题:

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

【讨论】:

    【解决方案2】:

    我使用 Spring 3.2.3 版本。试试这个配置:

    正确的 web.xml:

    <servlet-mapping>
    <servlet-name>Spring</servlet-name> 
    <url-pattern>*.html</url-pattern> 
    </servlet-mapping>
    

    正确的 Spring-servlet.xml:

    <context:component-scan base-package="your.package.controller"/>
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
    <bean id="internalViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:viewClass="org.springframework.web.servlet.view.JstlView"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp"/>
    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
    

    正确的映射注释:

    @RequestMapping(value="/index", method=RequestMethod.GET)
    

    并创建 index.jsp 文件。

    【讨论】:

    • 我已经更改了代码以反映您的 - 我仍然得到相同的 404 和相同的 WARN。
    【解决方案3】:

    当我的一个朋友建议我检查包装声明时,我也遇到了同样的问题。
    在我知道控制器请求映射和 context:component-scan base-package 的定义对于哪个包进行检查后,我发现它不一样,并且在给它正确的包名称后它开始工作。

    在你的 Spring 配置文件中检查以下 sn-p

    <context:component-scan base-package="your package name" > 
    

    【讨论】:

    • 我只是尝试使用 spring mvc 打印 hello world。它是一个非常简单的应用程序,我的示例已经发布。我的简单动机是为那些制作 spring 第一个应用程序但他们面临获得输出的问题的人.我不知道你做错了什么,所以减少我的投票。最好你访问我今天发布的堆栈溢出帖子。
    • 您为控制器类定义的包以及您应该在 spring.xml 文件中提及的包作为属性,即
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    • 2012-09-14
    • 2011-01-18
    相关资源
    最近更新 更多