java - 如何解决 HTTP 404 源服务器未找到目标资源的当前表示

标签 java eclipse spring-mvc

在 Skillsoft 上进行软件演示后,我在 Eclipse 中构建了一个简单的 Spring MVC 演示应用程序。该应用程序加载良好,我可以访问主页(“Hello World”)。但是当我尝试点击 Controller 时,我收到以下错误消息

HTTP 状态 404 - 未找到

类型状态报告

消息/springMVCDemo/WEB-INF/jsp/quote.jsp

描述源服务器未找到目标资源的当前表示或不愿意透露该表示的存在。

我研究了以下描述相同错误的链接,但我无法使我的代码工作:

Tomcat 404 error: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists

Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"

The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. on deploying to tomcat

The origin server did not find a current representation for the target resource or is not willing to disclose that one exists

web.xml

<web-app>
    <display-name>Archetype Created Web Application</display-name> 
    <servlet>
    <servlet-name>MyDemoApp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/myDemoApp-servletConfig.xml</param-value>
    </init-param>
    </servlet>

    <servlet-mapping>
    <servlet-name>MyDemoApp</servlet-name>
    <url-pattern>*.html</url-pattern>
    </servlet-mapping>
</web-app>

myDemoApp-servletConfig.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.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-4.0.xsd">

<mvc:annotation-driven/>
<context:component-scan base-package="com.demo.controllers"</context:component-scan>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>
</beans>

MyDemoController.java

package com.demo.controllers;

import java.util.Random;

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

@Controller
public class MyDemoController {
private String[] quotes = {"To be or not to be -Shakespeare",
                            "Stay hungry you're alone -Dee Snider",
                            "Might as well jump! -David Lee Roth"};

//http://localhost:8080/springMVCDemo/getQuote.html
@RequestMapping(value="/getQuote")
public String getRandomQuote(Model model) {
    int rand = new Random().nextInt(quotes.length);
    String randomQuote = quotes[rand];

    model.addAttribute("randomQuote", randomQuote);
    return "quote";
}

}

Quote.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>My Demo App</title>
</head>
<body>

<h1>The quote is:</h1>
<p>${randomQuote}</p>

</body>
</html> 

index.jsp

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

目录结构

springMVCDemo
|
-->main
   -->java
      -->com
         -->demo
            -->controllers
               -->MyDemoController.java
-->webapp
   -->WEB-INF
      myDemoApp-servletConfig.xml
      web.xml
      index.jsp
      -->jsp
         -->Quote.jsp

我的尝试和结果

实验#1:

http://localhost:8080/springMVCDemo

结果:浏览器显示“Hello World!”正如预期的那样

实验#2:

http://localhost:8080/springMVCDemo/getQuote.html

预期行为:显示三个引号之一

实际结果:HTTP 404 + Eclipse 控制台消息:

Dec 29, 2018 5:59:40 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'MyDemoApp'
Dec 29, 2018 5:59:40 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'MyDemoApp': initialization started
Dec 29, 2018 5:59:40 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'MyDemoApp-servlet': startup date [Sat Dec 29 17:59:40 EST 2018]; root of context hierarchy
Dec 29, 2018 5:59:40 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/myDemoApp-servletConfig.xml]
Dec 29, 2018 5:59:42 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
INFO: Mapped "{[/getQuote],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.demo.controllers.MyDemoController.getRandomQuote(org.springframework.ui.Model)
Dec 29, 2018 5:59:42 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'MyDemoApp': initialization completed in 1751 ms

实验#3:

http://localhost:8080/springMVCDemo/getQuote2.html

结果:正如预期的那样,我得到了 HTTP 404,因为不存在这样的映射。还显示以下 Eclipse 控制台消息:

Dec 29, 2018 6:03:21 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/springMVCDemo/getQuote2.html] in DispatcherServlet with name 'MyDemoApp'

总之,我相信我的 Controller 已正确映射,但由于某种原因,页面不会显示:(

我正在使用的软件版本

Eclipse 2018-09 (4.9.0); 汤姆猫v9.0

如果有人可以提供一些故障排除建议,我们将不胜感激。我花了近 8 个小时尝试定制我的 servlet 配置文件,但到目前为止还没有成功。

最佳答案

我注意到你的代码中的一些事情:

  1. 您的 Controller 的映射是 getQuote,它似乎是根据您服务器的此日志进行配置和运行的:

INFO: Mapped "{[/getQuote],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.demo.controllers.MyDemoController.getRandomQuote(org.springframework.ui.Model)

  • 您的jsp名称是Quote,而不是quote(区分大小写):
  • -->jsp -->Quote.jsp

    首先,您应该向映射的 URL 发出请求,在 getQuote 的情况下,如下所示:

    http://localhost:8080/springMVCDemo/getQuote

    在 Controller 中放置一个断点,并检查是否可以通过请求此 URL 实际到达它。

    还要修复 Controller 的返回页面,因为您的页面名为 Quote,所以您将返返回价页面(请记住区分大小写):

    @RequestMapping(value="/getQuote")
    public String getRandomQuote(Model model) {
        ...
        return "quote";
    }
    

    关于java - 如何解决 HTTP 404 源服务器未找到目标资源的当前表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53974110/

    相关文章:

    java - 向所有 swagger 请求添加 header

    java - 在 java spring hibernate 3.6.3 中如何告诉方法等待直到获取锁以及如何重试失败的事务?

    检查是否为空后的Java空指针异常

    java - Eclipse 氧气升级 - "Java Virtual Machine Launcher - A Java Exception has occured)"

    eclipse - 设置项目特定的Maven存储库

    java - Spring MVC : Controller RequestMapping working, 但返回总是给出 404

    java - 从连接池获取数据库连接

    c++ - eclipse 不会运行我的 exe 文件

    spring-mvc - 将 Spring 3 @ExceptionHandler 与公共(public) FileUpload 和 SizeLimitExceededException/MaxUploadSizeExceededException 一起使用

    java - 在 MessageSource 中找不到消息