SpringMVC HTTP状态404 -

时间:2017-06-12 14:37:46

标签: java spring-mvc intellij-idea

我开始学习 Spring MVC ,但是当我运行程序时,我收到以下错误:

  

HTTP状态404 -   类型状态报告   信息   description请求的资源不可用。   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会成为我遇到错误的原因吗?

1 个答案:

答案 0 :(得分:1)

请更改<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";
    }
}