Reputation: 99
I'm new to Spring MVC. While trying for basic program I'm facing the 404 error page.
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>My Demo App</display-name>
<servlet>
<servlet-name>myDemoApp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/myDemoApp-servletConfig.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>myDemoApp</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
myDemoApp-servletConfid.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
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-4.0.xsd">
<mvc:annotation-driven/>
<!-- Components Scanner...package name should be the one which is having the controllers -->
<context:component-scan base-package="com.demo.controllers"></context:component-scan>
<!-- View Resolver Interface -->
<!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean> -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
</beans>
Controller class
package com.demo.controllers;
import java.util.Random;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyDemoController {
private String[] quotes = {"Hi I am vikram,"
+ "I am from Bangalore,"
+ "Bangalore is full of traffic"};
@RequestMapping("/getQuote")
public String getRandomQuote(Model model){
int rand = new Random().nextInt(quotes.length);
String randomQuote = quotes[rand];
//http://localhost:8080/springMVCDemo/getQuote.html
model.addAttribute("randomQuote",randomQuote);
return "quote";
}
}
JSP Page
<%@ 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>My Demo App</title>
</head>
<body>
<h1>The Quote is: </h1>
<p>${randomQuote}</p>
</body>
</html>
URL Used:
http://localhost/8050/springMVCDemo/getQuote.html
Folder structure: [enter image description here][1]
Kindly suggest me the solution for the error.
Upvotes: 0
Views: 1031
Reputation: 2477
changes the myDemoApp (dispatcher) servlet mapping as below.
<servlet-mapping>
<servlet-name>myDemoApp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Upvotes: 0
Reputation: 46841
It's clear from the error that spring was failed to load the controller as per your current configuration. Always keep an eye on debug logs and look what beans are registered with spring container on start-up. Logging will help you better to solve this problem.
Try with Bootstrap listener ContextLoaderListener
that will read and load the configuration from contextConfigLocation
set in context-param
For example:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/myDemoApp-servletConfid.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Follow the related post
Upvotes: 1