使用Spring-webmvc映射问题

时间:2014-01-26 19:13:33

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

在设置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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

我使用的是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"/>

正确映射annotacion:

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

创建index.jsp文件。