0

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

Shawn
  • 13
  • 4
  • Are you required to use an external Tomcat container by, for example, an instructor, or do you have choice on how to deploy? – chrylis -cautiouslyoptimistic- Jul 24 '19 at 17:09
  • i deploy it using maven using the command "tomcat7:run" – Shawn Jul 24 '19 at 17:33
  • Can you decide not to use Tomcat, or does someone else tell you to use Tomcat? – chrylis -cautiouslyoptimistic- Jul 24 '19 at 17:34
  • tomcat is a popular choice for the SpringMVC framework and used by most tutorials out there. Is there a known problem for tomcat7 which you discourage me from using it? – Shawn Jul 24 '19 at 17:45
  • But if i am able to proceed till this point https://i.stack.imgur.com/hVUsv.png it shows that tomcat7 is working fine right? its just that it couldnt find any resource specified – Shawn Jul 24 '19 at 17:46
  • Using an external container *at all* is considered obsolete; the modern way to develop Spring MVC applications is to use Spring Boot, which handles all of this configuration for you and eliminates many errors. (I also recommend using Thymeleaf instead of JSP for HTML pages, because it is easier to work with and debug.) – chrylis -cautiouslyoptimistic- Jul 24 '19 at 19:56

1 Answers1

0

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>
Anthony
  • 571
  • 3
  • 11