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
HTTP Status 404 - Not Found
Type Status Report
Message /springMVCDemo/WEB-INF/jsp/quote.jsp
Description The 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:
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>
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:
http://localhost:8080/springMVCDemo
Result: Browser displays "Hello World!" as expected
Experiment #2:
http://localhost:8080/springMVCDemo/getQuote.html
Expected behaviour: One of three quotes gets displayed
Actual result: HTTP 404 + Eclipse console messages:
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:
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:
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
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.