Java 如何解决 HTTP 404 源服务器未找到目标资源的当前表示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53974110/
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
How to troubleshoot HTTP 404 The origin server did not find a current representation for the target resource
提问by Jason D
Following a software demo on Skillsoft, I built a simple Spring MVC Demo app in Eclipse. The app loads fine and I can hit the home page ("Hello World"). But when I try to hit my controller, I get the following error message
根据 Skillsoft 上的软件演示,我在 Eclipse 中构建了一个简单的 Spring MVC 演示应用程序。该应用程序加载良好,我可以点击主页(“Hello World”)。但是当我尝试点击我的控制器时,我收到以下错误消息
HTTP Status 404 - Not Found
HTTP 状态 404 - 未找到
TypeStatus Report
类型状态报告
Message/springMVCDemo/WEB-INF/jsp/quote.jsp
留言/springMVCDemo/WEB-INF/jsp/quote.jsp
DescriptionThe origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
描述源服务器没有找到目标资源的当前表示,或者不愿意透露一个存在。
I've studied the following links that describe the same error, but I could not make my code work:
我研究了以下描述相同错误的链接,但我无法使我的代码工作:
Tomcat 404 错误:源服务器没有找到目标资源的当前表示或不愿意透露一个存在
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
Servlet 返回“HTTP 状态 404 请求的资源 (/servlet) 不可用”
源服务器没有找到目标资源的当前表示,或者不愿意透露一个存在。关于部署到 tomcat
web.xml
网页.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
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
我的演示控制器.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
引用.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
索引.jsp
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
Directory Structure
目录结构
springMVCDemo
|
-->main
-->java
-->com
-->demo
-->controllers
-->MyDemoController.java
-->webapp
-->WEB-INF
myDemoApp-servletConfig.xml
web.xml
index.jsp
-->jsp
-->Quote.jsp
What I've tried and the results
我的尝试和结果
Experiment #1:
实验#1:
http://localhost:8080/springMVCDemo
Result: Browser displays "Hello World!" as expected
结果:浏览器显示“Hello World!” 正如预期的那样
Experiment #2:
实验#2:
http://localhost:8080/springMVCDemo/getQuote.html
Expected behaviour: One of three quotes gets displayed
预期行为:显示三个引号之一
Actual result: HTTP 404 + Eclipse console messages:
实际结果: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
Experiment #3:
实验#3:
http://localhost:8080/springMVCDemo/getQuote2.html
Result: As expected here, I got HTTP 404 since no such mapping exists. Also the following Eclipse console message was displayed:
结果:正如预期的那样,我得到了 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'
In summary, I believe my controller is getting mapped correctly, but for some reason the page won't get displayed :(
总之,我相信我的控制器已正确映射,但由于某种原因该页面不会显示:(
Software versions I am using
我使用的软件版本
Eclipse 2018-09 (4.9.0); Tomcat v9.0
Eclipse 2018-09 (4.9.0);雄猫 v9.0
If anyone can provide some troubleshooting advice, it would be greatly appreciated. I've spent nearly 8 hours trying to tailor my servlet config file, but so far no luck.
如果有人可以提供一些故障排除建议,将不胜感激。我花了将近 8 个小时试图定制我的 servlet 配置文件,但到目前为止还没有运气。
采纳答案by nullptr
I noticed a few things along your code:
我注意到你的代码中有几件事:
- The mapping of your controller is
getQuote
, and it seems to be configured and running based on this log of your server:
- 你的控制器的映射是
getQuote
,它似乎是根据你服务器的这个日志配置和运行的:
INFO: Mapped "{[/getQuote],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.demo.controllers.MyDemoController.getRandomQuote(org.springframework.ui.Model)
信息:将“{[ /getQuote],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}”映射到公共 java.lang.String com。 demo.controllers.MyDemoController.getRandomQuote(org.springframework.ui.Model)
- Your jsp name is
Quote
, instead ofquote
(case sensitive matters):
- 您的 jsp 名称是
Quote
, 而不是quote
(区分大小写的事项):
-->jsp -->Quote.jsp
-->jsp -->引用.jsp
So first, you should do a request to your mapped URL, in the case getQuote
as the following:
因此,首先,您应该向映射的 URL 发出请求,getQuote
如下所示:
Put a breakpoint in your controller and check if you can actually reach it by requesting this URL.
在您的控制器中放置一个断点并检查您是否可以通过请求此 URL 来实际访问它。
Also fix the return page of your Controller, since your page is named Quote, you are returning the quote page instead (remember the case sensitive):
还要修复您的控制器的返回页面,因为您的页面名为 Quote,因此您将返回引用页面(请记住区分大小写):
@RequestMapping(value="/getQuote")
public String getRandomQuote(Model model) {
...
return "quote";
}