2017-06-12 235 views
-1

我開始學習Spring MVC的,但是當我運行我的程序,我收到以下錯誤:HTTP狀態404 -

HTTP Status 404 - type Status report message description The requested resource is not available. Apache Tomcat/8.0.41

web.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
     version="3.1"> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/applicationContext.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <servlet> 
     <servlet-name>springmvc</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/springmvc-servlet.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>springmvc</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

用SpringMVC-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.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
     http://www.springframework.org/schema/cache 
     http://www.springframework.org/schema/cache/spring-cache.xsd"> 

    <context:component-scan base-package="com"/> 
    <mvc:annotation-driven/> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/view/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 
</beans> 

SpringTest.java

package com; 

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

@Controller 
@RequestMapping("/home") 
public class SpringTest { 
    @RequestMapping(value = "/home",method =RequestMethod.GET) 
    public String home(){ 
     return "home"; 
    } 
} 

項目結構如下:

enter image description here

我用的IntelliJ IDEA來運行這個程序。 IntelliJ IDEA會成爲我收到錯誤的原因嗎?

+0

它不會失敗時你運行程序。發送HTTP請求時失敗。你發送了什麼HTTP請求? –

+0

我使用Tomcat來test.So,我不知道HTTP請求發送我謝謝 –

回答

0

請改變<context:component-scan base-package="com"/><mvc:annotation-driven/>之間的順序使<mvc:annotation-driven/>第一

您試圖訪問${ContextPath}/home/home,請嘗試以下通過代碼來替換你的控制器:

@Controller 
public class SpringTest { 
    @RequestMapping(value = "/home",method =RequestMethod.GET) 
    public String home(){ 
     return "home"; 
    } 
} 
+0

你,但我試過之後還是not.I'm快要瘋了 –