java - 如何解决 HTTP 404 源服务器未找到目标资源的当前表示
问题描述
在 Skillsoft 上进行软件演示之后,我在 Eclipse 中构建了一个简单的 Spring MVC Demo 应用程序。该应用程序加载正常,我可以点击主页(“Hello World”)。但是当我尝试点击我的控制器时,我收到以下错误消息
HTTP 状态 404 - 未找到
类型状态报告
消息/springMVCDemo/WEB-INF/jsp/quote.jsp
描述源服务器没有找到目标资源的当前表示或不愿意透露存在的表示。
我研究了以下描述相同错误的链接,但我无法使我的代码工作:
Tomcat 404 错误:源服务器没有找到目标资源的当前表示或不愿意透露存在的表示
Servlet 返回“HTTP 状态 404 请求的资源 (/servlet) 不可用”
源服务器没有找到目标资源的当前表示或不愿意透露存在的表示。关于部署到 tomcat
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";
}
}
报价单.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>
索引.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'
总之,我相信我的控制器已正确映射,但由于某种原因页面不会显示:(
我正在使用的软件版本
日食 2018-09 (4.9.0); 雄猫 v9.0
如果有人可以提供一些故障排除建议,将不胜感激。我花了将近 8 个小时来尝试定制我的 servlet 配置文件,但到目前为止还没有运气。
解决方案
我注意到您的代码中有一些事情:
- 您的控制器的映射是
getQuote
,并且它似乎是根据您的服务器的此日志配置和运行的:
信息:将“{[ /getQuote ],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}”映射到公共 java.lang.String com。 demo.controllers.MyDemoController.getRandomQuote(org.springframework.ui.Model)
- 您的 jsp 名称是
Quote
,而不是quote
(区分大小写的事项):
-->jsp -->Quote.jsp
因此,首先,您应该向映射的 URL 发出请求,getQuote
如下所示:
在您的控制器中放置一个断点,并检查您是否可以通过请求此 URL 实际到达它。
还要修复你的控制器的返回页面,因为你的页面被命名为引用,你正在返回引用页面(记住区分大小写):
@RequestMapping(value="/getQuote")
public String getRandomQuote(Model model) {
...
return "quote";
}
推荐阅读
- ssl - 在 JMeter 中永久使用 p12 文件
- java - Swing - 在节点删除时调用 JTree addTreeSelectionListener
- javascript - 对 https://registry.npmjs.org/yargs 的请求失败
- performance - JMeter - 计算响应低于定义时间的请求
- java - 使用 Spring 缓存 REST 服务
- tabs - 错误:给定选项卡没有剪辑信息
- html - 悬停更改背景颜色但不更改文本颜色
- css - 网站无法在 https 上正确加载
- ios - 在 FSCalender Objective-c 中隐藏月份占位符
- angular - 角度材质选项卡控件 - RTL