Java 春天的MVC。HTTP 状态 404

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21499695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:01:41  来源:igfitidea点击:

Spring MVC. HTTP Status 404

javaspringjspspring-mvcmodel-view-controller

提问by DmitMedv

I began to learn "Spring MVC" from this course: http://www.pluralsight.com/training/Courses/TableOfContents/springmvc-introAt step "Building->Run the Application" I'm stuck.

我从这门课程开始学习“Spring MVC”:http: //www.pluralsight.com/training/Courses/TableOfContents/springmvc-intro在步骤“构建->运行应用程序”我被卡住了。

When I try to go to link http://localhost:8080/FitnessTracker/greeting.htmlI get "HTTP Status 404"

当我尝试转到链接时,http://localhost:8080/FitnessTracker/greeting.html我收到“HTTP 状态 404”

HTTP Status 404 - /FitnessTracker/WEB-INF/jsp/Hello.jsp
type Status report
message /FitnessTracker/WEB-INF/jsp/Hello.jsp
description The requested resource is not available.
Apache Tomcat/7.0.50

web.xml

网页.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
  <servlet>
    <servlet-name>fitTrackerServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/config/servlet-config.xml</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>fitTrackerServlet</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>

  <display-name>Archetype Created Web Application</display-name>
</web-app>

servlet-config.xml

servlet-config.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"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        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-3.2.xsd">

  <mvc:annotation-driven />
  <context:component-scan base-package="com.pluralsight.controller"></context:component-scan>

  <!--
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
  </bean>
  -->

  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>

</beans>

HelloController.java

你好控制器.java

package com.pluralsight.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

    @RequestMapping(value = "/greeting")
    public String sayHello (Model model) {
        model.addAttribute("greeting", "Hello WorldX");
        return "Hello"; 
    }
}

hello.jsp

你好.jsp

<%@ page language="java" contentType="text/html; charset=US-ASCII"
    pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Insert title here</title>
</head>
<body>
  <h1>${greeting}</h1>
</body>
</html>

enter image description here

在此处输入图片说明

http://localhost:8080/FitnessTracker/- it's work (file: webapp/index.jsp)

http://localhost:8080/FitnessTracker/- 它的工作(文件:webapp/index.jsp)

采纳答案by JB Nizet

The error message says that it's looking for Hello.jsp. And indeed, your controller returns the following view name: "Hello". But your screenshot shows that your file is naled hello.jsp. The case matters.

错误消息说它正在寻找Hello.jsp. 事实上,你的控制器返回以下视图名称:"Hello"。但是您的屏幕截图显示您的文件已 naled hello.jsp。案件很重要。

回答by Kevin Bowersox

Try returning the properly cased view name which matches the JSP hello.jspfrom the controller. Currently the controller returns Hellowhile the JSP is named hello.jsp, causing the 404.

尝试hello.jsp从控制器返回与 JSP 匹配的大小写正确的视图名称。当前控制器Hello在 JSP 被命名时返回hello.jsp,导致 404。

@Controller
public class HelloController {

    @RequestMapping(value = "/greeting")
    public String sayHello (Model model) {
        model.addAttribute("greeting", "Hello WorldX");
        //return "Hello"; 
        return "hello"; 
    }
}

回答by Tunde Pizzle

Change in your web.xml the to

将您的 web.xml 更改为

<servlet-mapping>
   <servlet-name>fitTrackerServlet</servlet-name>
   <url-pattern>/</url-pattern>
</servlet-mapping>