1

I really need help with this. I am practicing developing an application based on Spring MVC with JSP. I am starting from the basic and trying to print the "page.jsp" content but it always prints "index.html" content. If I delete "index.html" then I get a 404 error. It seems that the Controller class is not getting scanned. I am stuck here and I am not finding any solution. Following is my code:

web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
  <display-name>Archetype Created Web Application</display-name>

    <!-- CONFIGURING FRONT CONTROLLER -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern> 
    </servlet-mapping>

</web-app>

PageController.java

package net.ritz.onlineshopping.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class PageController {
    
    @RequestMapping(value= {"/","/home","/index"})
    public ModelAndView index() {   
        ModelAndView mv = new ModelAndView("page");
        mv.addObject("greeting", "Welcome to Spring Web MVC");
        return mv;
    }
}

page.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Online Shopping</title>
</head>
<body>
    ${greeting}
</body>
</html>

dispatcher-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   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.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
   
   <mvc:annotation-driven  />
   <context:component-scan base-package="net.ritz.onlineshopping.controller"/>
   
   <bean id="viewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
              <property name="prefix" value="/WEB-INF/views/"/>
              <property name="suffix" value=".jsp"/>
        </bean>
   
   
   
   </beans>
   

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
This is Index
</body>
</html>

When I enter this URL in the browser: http://localhost:8080/onlineshopping/

I get the following Output which is index.html but it should map to page.jsp: enter image description here

Below is my project structure: enter image description here

Ritesh Puri
  • 305
  • 2
  • 11

3 Answers3

2

After doing a lot of research, I found an answer to this annoying problem. So, right click on the project, then click on properties. Select "Deployment Assembly" and then click on "Add" button. Select "Java Build Path Entries", click on Next, then select "Maven Dependencies" and click on OK. Right click on the Tomcat Server under the Servers section and select "Clean". Right click on the Tomcat Server again and select "Publish". I ran my project on Tomcat Server and 404 not found error was resolved. I was able to print page.jsp content!!

Ritesh Puri
  • 305
  • 2
  • 11
0

Double check your mapping and see if it's work. You can debug or add more log to make sure:

    @RequestMapping(value= {"/","/home","/index"})
    public ModelAndView index() {   
        ModelAndView mv = new ModelAndView("page");
        mv.addObject("greeting", "Welcome to Spring Web MVC");
        return mv;
    }

Basically a @Controller without is, well, pretty useless as it does nothing but take up memory. It will not be bound to incoming requests, it just hangs around in the application context. It is just another bean like all other beans and nothing special is being done to it. (Recent, but deprecated, versions of Spring register the DefaultAnnotationHandlerMapping which processes the @Controller, this is however deprecated).

Spring support for @Controller given by <context:component-scan /> vs <mvc:annotation-driven>

Try adding below config in your setting.

<context:annotation-config/> <!-- is used to activate the annotation for beans -->
<mvc:annotation-driven/>
Huy Nguyen
  • 1,931
  • 1
  • 11
  • 11
  • It seems the project by default needs to map to index.html first. If I don't have this file then I get an error message else the index.html content is displayed even though the URL should map to page.jsp as per the code you have shown. – Ritesh Puri Sep 22 '18 at 13:22
  • I am not sure if all setting is correct, so i doubt about it. I suggest you can try mapping other url like "/test" for testing purpose and return a view page and see how it work. – Huy Nguyen Sep 22 '18 at 13:32
  • I did try mapping to different endpoint (/test) but still got the same issue. – Ritesh Puri Sep 22 '18 at 13:47
  • Yeah, it seem component scan not work for @Controller, you should add more and recheck https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-config – Huy Nguyen Sep 22 '18 at 14:41
  • I have already tried this as suggested as a solution somewhere else but it didn't work for me but I do agree with you on component scan not able to scan my controller class. – Ritesh Puri Sep 22 '18 at 15:17
  • just update and give more short description about mvc:annotation-driven, also link the article explain for it. – Huy Nguyen Sep 22 '18 at 15:49
  • @huy - I have updated my dispatcher-servlet.xml file with mvc-annotation drivern as shown above but still didn't resolve the issue. – Ritesh Puri Sep 22 '18 at 17:30
  • i updated the answer. Please try adding – Huy Nguyen Sep 23 '18 at 05:25
  • @huy - I have actually solved the issue. I have added a separate comment. – Ritesh Puri Sep 28 '18 at 23:36
0

You can try one of the below solutions.

Check your xml configuration whether any <welcome-file-list>index.html</welcome-file-list> is present if yes then remove that.

OR

Try deleting the index.html file and check whether controller returns jsp page.

OR

Create a different endpoint and check whether controller returns jsp page like below.

@RequestMapping(value="/test")
    public ModelAndView test() {   
        ModelAndView mv = new ModelAndView("page");
        mv.addObject("greeting", "Welcome to Spring Web MVC");
        return mv;
    }
Alien
  • 15,141
  • 6
  • 37
  • 57
  • First, I don't have tag defined. Second, if I delete index.html file, then I get "404 Not Found" error with the description: "The origin server did not find a current representation for the target resource or is not willing to disclose that one exists." Can you help me with the third point? – Ritesh Puri Sep 22 '18 at 13:19
  • I included this code and ran "http://localhost:8080/onlineshopping/test" but still got the same 404 not found error. – Ritesh Puri Sep 22 '18 at 13:44
  • Are you able to hit controller using test? – Alien Sep 22 '18 at 13:49
  • No, I am not. I suspect if defined in dispatcher-servlet.xml is working properlty. If I enter the url "http://localhost:8080/onlineshopping/", then it should display page.jsp content but instead it maps to index.html if this file is defined else a 404 not found error. – Ritesh Puri Sep 22 '18 at 13:56