【问题标题】:SpringMVC HTTP Status 404 -SpringMVC HTTP 状态 404 -
【发布时间】:2017-06-12 14:37:46
【问题描述】:

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

HTTP 状态 404 - 类型状态报告 信息 描述 请求的资源不可用。 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";
    }
}

我的项目结构如下:

我使用 IntelliJ IDEA 来运行这个过程。 IntelliJ IDEA 会是我遇到错误的原因吗?

【问题讨论】:

  • 运行程序时不会失败。当您发送 HTTP 请求时,它会失败。你发送了什么 HTTP 请求?
  • 我使用Tomcat进行测试。所以,我不知道我发送的HTTP请求

标签: java spring-mvc intellij-idea


【解决方案1】:

请更改&lt;context:component-scan base-package="com"/&gt;&lt;mvc:annotation-driven/&gt;之间的顺序,使&lt;mvc:annotation-driven/&gt;成为第一个

您正在尝试访问${ContextPath}/home/home,请尝试用以下代码替换您的控制器:

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

【讨论】:

  • 谢谢,但不管我是否尝试过,我都快疯了
猜你喜欢
  • 2015-11-04
  • 1970-01-01
  • 1970-01-01
  • 2018-04-13
  • 2018-03-05
  • 1970-01-01
  • 2015-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多