2017-06-12 208 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快要疯了 –