java - Spring MVC。 HTTP 状态 404

标签 java spring jsp spring-mvc model-view-controller

我从这门类(class)开始学习“Spring MVC”:http://www.pluralsight.com/training/Courses/TableOfContents/springmvc-intro 在“构建->运行应用程序”步骤中,我陷入了困境。

当我尝试访问链接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 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

<?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

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

<%@ 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/ - 工作正常(文件:webapp/index.jsp)

最佳答案

错误消息表明它正在寻找 Hello.jsp。事实上,您的 Controller 返回以下 View 名称:“Hello”。但您的屏幕截图显示您的文件名为 hello.jsp。案件很重要。

关于java - Spring MVC。 HTTP 状态 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21499695/

相关文章:

java - 具有计数条件的 Hibernate 条件

java - 如何将 URL 作为路径变量传递

java - servlet编程如何将jsp中applet的内容保存到数据库中

javascript - ajax 响应中未调用 Dojo 就绪函数

java - 未找到 Jersey 类...但它就在那里

java - 当我在 eclipse gwt 项目中运行我的代码时,无法为 JSP 错误编译类

java - 重装操作系统后无法在Jmeter中记录

JavaFX - 在 ListView 中创建不可选择的项目

java - 我可以使用 spring DI 将 session 属性注入(inject)到我的 web 应用程序的 POJO 中吗

java - 使用 spring data mongodb 存储库添加可选查询参数