SpringMVC - URL resource couldnt be found

37 views Asked by At

I am a student working on the spring MVC framework. apologies in advance if this is a bad question

I am using maven for deployment to tomcat7 I understood the flow of which how springMVC works and I will try to explain it. Do correct any misunderstandings I have

  1. the client request will hit web.xml first

  2. In web.xml, URL-patterns is tied to a servletName(or DispatcherServlet) & that will map it to (by default) another xml named "[servletName]-servlet.xml" (i can also instantiate it if i want it to be of another name)

  3. In [servletName]-servlet.xml, it will concatenate the prefix and suffix to the URL so that it becomes a filePath (eg. /WEB-INF/jsp/ + hello + .jsp) to locate the correct jspFile to render in client browser

  4. A controller file (declared @Controller) acts as the backend and is invoked using @RequestMapping(URL)

Base on this flow, i type "localhost:8080/hello.jsp" (have also tried other URL mentioned below) but it still returns HTTP404

tomcat7 is running fine just that the jsp pages cant be found

I have tried editing the "url-patterns" in web.xml to "/" OR ".jsp" OR "hello.jsp" OR "hello" but to no avail

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>to do list</display-name>

  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
   </context-param>

   <listener>
      <listener-class>
         org.springframework.web.context.ContextLoaderListener
      </listener-class>
   </listener>

    <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>

</web-app>

HelloWeb-servlet.xml

<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"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:component-scan base-package = "com.tutorialspoint" />

   <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name = "prefix" value = "/WEB-INF/jsp/" />
      <property name = "suffix" value = ".jsp" />
   </bean>

</beans>

hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
   <head>
      <title>Hello Spring MVC</title>
   </head>

   <body>
      <h2>${message}</h2>
   </body>
</html>

HelloController.java

@Controller
@RequestMapping("/hello")
public class HelloController {
    @RequestMapping(method = RequestMethod.GET)
       public String printHello(ModelMap model) {
          model.addAttribute("message", "Hello Spring MVC Framework!");
          return "hello";
       }
}

this is my file hierarchy

https://i.stack.imgur.com/msr7k.png

and this is the error

https://i.stack.imgur.com/hVUsv.png

1

There are 1 answers

0
Anthony On

Try to use this code configuration for servlet. Do you try access to http://localhost:8080/hello ?

       <servlet>
          <servlet-name>HelloWeb</servlet-name>
          <servlet-class>
             org.springframework.web.servlet.DispatcherServlet
          </servlet-class>
          <init-param>
                <param-name>contextConfigLocation</param-name>
               <param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
            </init-param>
          <load-on-startup>1</load-on-startup>
       </servlet>