Issue
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:
Below is my project structure:
Solution
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!!
Answered By - Ritesh Puri