在Skillkill上进行软件演示之后,我在Eclipse中构建了一个简单的Spring MVC Demo应用程序。该应用程序加载正常,我可以访问主页(“ Hello World”)。但是,当我尝试按下控制器时,会收到以下错误消息
HTTP状态404-找不到
类型状态报告
消息 /springMVCDemo/WEB-INF/jsp/quote.jsp
描述:原始服务器找不到目标资源的当前表示,或者不愿意透露其存在。
我研究了以下描述相同错误的链接,但是我无法使我的代码正常工作
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
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'
总而言之,我相信我的控制器已正确映射,但是由于某些原因,该页面无法显示:(
我正在使用的软件版本
Eclipse 2018-09(4.9.0); Tomcat v9.0
如果任何人都可以提供一些故障排除建议,将不胜感激。我花了将近8个小时来尝试定制servlet配置文件,但到目前为止还没有运气。
答案 0 :(得分:0)
我注意到您的代码中有几件事:
getQuote
,它似乎是根据服务器的以下日志配置并运行的:INFO:映射 “ {[ / getQuote ],方法= [],params = [],标题= [],consumes = [],produces = [],custom = []}” 到公共java.lang.String上 com.demo.controllers.MyDemoController.getRandomQuote(org.springframework.ui.Model)
Quote
,而不是quote
(区分大小写):-> jsp -> Quote.jsp
因此,在getQuote
的情况下,首先应向映射的URL发出请求:
在控制器中放置一个断点,并通过请求此URL检查您是否真的可以到达它。
还修复了Controller的返回页面,因为您的页面名为Quote,所以您返回的是报价页面(请注意区分大小写):
@RequestMapping(value="/getQuote")
public String getRandomQuote(Model model) {
...
return "quote";
}