0

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://i.stack.imgur.com/sAzig.jpg

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?

1 Answers1

0

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 :

http://localhost/Latest/hello

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
  • I moved to this.But there is no luck. – Sayantan Chatterjee Dec 23 '18 at 05:59
  • Looks like your base package is incorrect. `base-package="com.mypackage"` – Nicholas K Dec 23 '18 at 06:07
  • please have a look at my post on Web.xml file. I have added HelloWeb / And tomcat server shows url http://localhost:8080/Latest/ but that's not correct. – Sayantan Chatterjee Dec 23 '18 at 06:43
  • How are you trying to access `printHello()`? What is the url you are hitting? Also your `base-package` is still incorrect. – Nicholas K Dec 23 '18 at 06:47
  • When I hit url http://localhost:8080/Latest/views/hello.jsp, then helo.jsp page is working fine. Bu when I select(Latest folder) -> right-click -> run on server(on Tomcat 9.0.8),error shows 'HTTP Status 404 – Not Found: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. – Sayantan Chatterjee Dec 23 '18 at 06:58
  • Yes it worked!! Thank you so much, very much appreciated. For your information, I don't understand my base-package is incorrect. I'd request you to please suggest me. – Sayantan Chatterjee Dec 23 '18 at 07:18
  • As per your screenshot it your controller code is present under `com.mypackage`, but as per your xml is says `com.tutorialspoint`. I don't see any such package in your screenshot. – Nicholas K Dec 23 '18 at 07:31
  • Ohh I overlooked to edit my post com.mypackage from com.tutorialspoint. – Sayantan Chatterjee Dec 23 '18 at 07:41