2014-01-26 182 views
1

在建立一個Spring應用程序(打包爲一個WAR和託管通過Tomcat的),我得到一個404和下面的錯誤,當我嘗試訪問「本地主機:8080」:與Spring-webmvc映射問題

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> 

此外,這裏是我的春天-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 
+0

下面的代碼片段你在瀏覽器中輸入網址是什麼? –

+0

我輸入了localhost:8080 – cscan

+0

請粘貼整個spring-servlet.xml –

回答

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文件。

+0

我已更改我的代碼以反映您的 - 我仍然以相同的404和相同的警告結束。 – cscan

1

無論出於何種原因,改變了我的web.xml這個的Servlet映射部分解決了這個問題:

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

我也面臨同樣的問題時,我的一個朋友建議我檢查包宣言。
當我知道哪個包的控制器請求映射和上下文的定義:component-scan base-package來檢查我看到它是不一樣的,並且在給它正確的包名後它開始工作。

檢查在Spring配置文件

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

我只是嘗試使用spring mvc.its打印hello world,這是一個非常簡單的應用程序,我的示例已經發布。我的簡單動機是爲那些在春季做第一次應用的人,但他們面臨的問題是獲得輸出。我不知道你錯了什麼,因此減少我的投票。更好的是你今天發佈的堆棧溢出中訪問我的帖子。 –

+0

您爲控制器類定義的軟件包以及該軟件包,您應該在spring.xml文件中作爲屬性提及,例如