Issue
I'm creating new spring mvc Dynamic Web Project on Eclipse version 4.9.0 Java EE IDE for Web Developers. I'm setting up a new Server Tomcat version 9.0.8.
When I select hello.jsp
and right-click ->Run As -> Run on Server
, then hello.jsp
works fine,and I access helo.jsp
. But When I select project root folder and right-click ->Run As -> Run
on Server, then it shows HTTP Status 404
with description:
The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.'
here is my Web.xml:
<web-app id = "WebApp_ID" version = "2.4"
xmlns = "http://java.sun.com/xml/ns/j2ee"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
Here is my full HelloWeb-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:context = "http://www.springframework.org/schema/context"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.tutorialspoint"/>
<mvc:annotation-driven/>
<bean name="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WebContent/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
And here is my Controller:
package com.mypackage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;
@Controller
public class HelloController {
@RequestMapping(path="/hello",method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
Here's my directory structure: https://imgur.com/CHKEsJo
here's my hello.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>Hello World</title>
</head>
<body>
<h2>done!!!</h2>
<h2>${message}</h2>
</body>
</html>
My server.xml on Tomcat version 9.0.8 is remained as it is. I haven't changed anything. Only thing I have changed the HTTP/1.1 port,AJP/1.3 port and Tomcat admin Port.
What's wrong with here and what's the reason for this? I can't find out why. Can anyone point my mistake here?
Solution
Try moving the folder views
under the /WEB-INF/
and then change your HelloWeb-servlet.xml
accordingly.
Also, make sure you are using this url to hit the resource :
Answered By - Nicholas K