Spring MVC Example

Filed Under: Spring

Welcome to Spring MVC Example. Sometime back in Spring MVC Tutorial, I explained how to create Spring MVC application using Spring Tool Suite. But today, I will be creating a basic hello world spring MVC application using maven and Eclipse.

Spring MVC Example

Spring MVC is based on Model-View-Controller architecture. Below image shows Spring MVC architecture at a high level.

spring mvc example, spring mvc architecture

DispatcherServlet is the front controller class to take all requests and start processing them. We have to configure it in web.xml file. It’s job is to pass request to appropriate controller class and send the response back when view pages have rendered the response page.

HomeController.java will be the single controller class in our spring mvc example application.

home.jsp, user.jsp are the view pages in our spring mvc hello world example application.

User.java will be the only model class we will have in our spring mvc example web application.

Spring MVC Example Hello World Eclipse Project

Below image shows our Spring MVC example project in Eclipse.

spring mvc hello world example eclipse project

Let’s get started and create our project right from the scratch.

Spring MVC Example Eclipse Project Setup

Since it’s a web application and we want to use maven for dependencies management, first of all we have to create a dynamic web application and then convert it to a maven project. Below images show how to do this and get our project skeleton structure ready.

Right click on the project explorer window and click on “New -> Dynamic Web Project” as shown in below image.

eclipse new dynamic web project

Provide name as “spring-mvc-example” in the next popup page, rest of the things should not required to be changed.

eclipse dynamic web project name for spring mvc example web application

On next page, provide the source folder as “src/main/java”. You might have to remove “src” folder from the list before adding this.

eclipse dynamic web project source folders

Next is the web module page, provide the context root of application as “spring-mvc-example” and make sure to check “Generate web.xml deployment descriptor” option.

eclipse dynamic web project web module context root

Click on Finish and you will have a new Dynamic Web Project in your eclipse project explorer.

Converting Dynamic Web Project to Maven Project

We want to use maven for easily manage our spring mvc dependencies. So let’s convert our web project to maven.

Right click on the project and select “Configure -> Convert to Maven Project”.
eclipse dynamic web project to maven project

Next provide the pom.xml configurations as shown below.

eclipse

Our maven web application project skeleton code is ready. Now we can start making changes to it and create our spring mvc hello world example application.

Spring MVC Dependencies to pom.xml

We need to add spring-web and spring-webmvc dependencies in pom.xml, also add servlet-api, jsp-api and jstl dependencies. Our final pom.xml file will be like below.


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.journaldev.spring.mvc</groupId>
	<artifactId>spring-mvc-example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>Spring MVC Example</name>
	<description>Spring MVC Hello World Example</description>

	<!-- Add Spring Web and MVC dependencies -->
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>
		<!-- Servlet -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.6.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>3.0.0</version>
				<configuration>
					<warSourceDirectory>WebContent</warSourceDirectory>
				</configuration>
			</plugin>
		</plugins>
		<finalName>${project.artifactId}</finalName> <!-- added to remove Version from WAR file -->
	</build>
</project>

Notice the finalName configuration in build, so that our WAR file name doesn’t have version details.

When the project is build by Eclipse, you will notice all the jars showing up in maven dependencies section.

Spring MVC DispatcherServlet as Front Controller

We have to add Spring MVC framework to our web application, for that we need to configure DispatcherServlet in web.xml as shown below.


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>spring-mvc-example</display-name>

	<!-- Add Spring MVC DispatcherServlet as front controller -->
	<servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
       		<param-name>contextConfigLocation</param-name>
       		<param-value>/WEB-INF/spring-servlet.xml</param-value>
    		</init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern> 
    </servlet-mapping>
    
 </web-app>

contextConfigLocation init-param is used to provide the location of spring bean configuration file.

Spring MVC Example Bean Configuration File

Next step is to create spring bean configuration file spring-servlet.xml as shown below.


<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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.xsd">

	<!-- DispatcherServlet Context: defines this servlet's request-processing 
		infrastructure -->

	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />
	<context:component-scan base-package="com.journaldev.spring" />

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
		in the /WEB-INF/views directory -->
	<beans:bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>

</beans:beans>

There are three important configurations.

  1. annotation-driven tells DispatcherServlet to look for Controller classes using @Controller annotation.
  2. context:component-scan tells DispatcherServlet where to look for controller classes.
  3. InternalResourceViewResolver bean configuration to specify location of view pages and suffix used. Controller class methods return name of the view page and then suffix is added to figure out the view page to use for rendering the response.

Spring MVC Controller Class

We have a single controller class to respond for two URIs – “/” for home page and “/user” for user page.


package com.journaldev.spring.controller;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.journaldev.spring.model.User;

@Controller
public class HomeController {

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		System.out.println("Home Page Requested, locale = " + locale);
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

		String formattedDate = dateFormat.format(date);

		model.addAttribute("serverTime", formattedDate);

		return "home";
	}

	@RequestMapping(value = "/user", method = RequestMethod.POST)
	public String user(@Validated User user, Model model) {
		System.out.println("User Page Requested");
		model.addAttribute("userName", user.getUserName());
		return "user";
	}
}

Note that for simplicity, I have not used any logging framework such as log4j.

Spring MVC Model Class

We have a simple model class with a single variable and it’s getter-setter methods. It’s a simple POJO class.


package com.journaldev.spring.model;

public class User {
	private String userName;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}
}

Spring MVC View Pages

We have two view pages as defined below.

home.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page session="false"%>
<html>
<head>
<title>Home</title>
</head>
<body>
	<h1>Hello world!</h1>

	<P>The time on the server is ${serverTime}.</p>

	<form action="user" method="post">
		<input type="text" name="userName"><br> <input
			type="submit" value="Login">
	</form>
</body>
</html>

user.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>User Home Page</title>
</head>
<body>
<h3>Hi ${userName}</h3>
</body>
</html>

Notice that Spring MVC takes care of mapping form variables to model class variables, that’s why we have same variable name in both places.

That’s it, our spring mvc example project is ready to de deployed and test.

Spring MVC Eclipse Project Deployment

We can use Eclipse export as WAR file option to deploy it directly to any running tomcat server webapps directory. However you can also use command line to build the project and then copy it into your favourite servlet container deployment directory.

eclipse maven project export as war file

eclipse project export to tomcat webapps directory

Spring MVC Example Test

Once the spring mvc project is deployed, we can access the home page at http://localhost:8080/spring-mvc-example/. Change the tomcat port and context-root accordingly.

spring mvc example hello world application home page

spring mvc example form post page

That’s all for Spring MVC example, I have tried to keep it as simple as possible. But still if you face any issues then please let me know through comments and I will try to help you out. You can download the final spring mvc example project from below link.

References: Official Page

Comments

  1. Surabhi agarwal says:

    Apr 17, 2019 12:42:38 AM org.apache.catalina.core.AprLifecycleListener init
    INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files (x86)\Java\jre1.8.0_51\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files (x86)/Java/jre1.8.0_51/bin/client;C:/Program Files (x86)/Java/jre1.8.0_51/bin;C:/Program Files (x86)/Java/jre1.8.0_51/lib/i386;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Java\jdk1.7.0\bin;C:\Program Files (x86)\Skype\Phone\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files\MySQL\MySQL Utilities 1.6\;C:\Program Files (x86)\Windows Live\Shared;F:\Setup\eclipse_new;;.
    Apr 17, 2019 12:42:38 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
    WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ‘source’ to ‘org.eclipse.jst.jee.server:spring-web-example’ did not find a matching property.
    Apr 17, 2019 12:42:38 AM org.apache.coyote.AbstractProtocol init
    INFO: Initializing ProtocolHandler [“http-bio-8080”]
    Apr 17, 2019 12:42:38 AM org.apache.coyote.AbstractProtocol init
    INFO: Initializing ProtocolHandler [“ajp-bio-8009”]
    Apr 17, 2019 12:42:38 AM org.apache.catalina.startup.Catalina load
    INFO: Initialization processed in 1301 ms
    Apr 17, 2019 12:42:38 AM org.apache.catalina.core.StandardService startInternal
    INFO: Starting service Catalina
    Apr 17, 2019 12:42:38 AM org.apache.catalina.core.StandardEngine startInternal
    INFO: Starting Servlet Engine: Apache Tomcat/7.0.37
    Apr 17, 2019 12:42:44 AM org.apache.catalina.core.ApplicationContext log
    INFO: No Spring WebApplicationInitializer types detected on classpath
    Apr 17, 2019 12:42:44 AM org.apache.catalina.util.SessionIdGenerator createSecureRandom
    INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [299] milliseconds.
    Apr 17, 2019 12:42:44 AM org.apache.catalina.core.ApplicationContext log
    INFO: Marking servlet spring as unavailable
    Apr 17, 2019 12:42:44 AM org.apache.catalina.core.StandardContext loadOnStartup
    SEVERE: Servlet /spring-web-example threw load() exception
    java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1713)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1558)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:527)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:509)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:137)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5033)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5317)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

    Apr 17, 2019 12:42:44 AM org.apache.catalina.startup.HostConfig deployDirectory
    INFO: Deploying web application directory F:\Setup\eclipse_new\tomcat\apache-tomcat-7.0.37\webapps\docs
    Apr 17, 2019 12:42:44 AM org.apache.catalina.startup.HostConfig deployDirectory
    INFO: Deploying web application directory F:\Setup\eclipse_new\tomcat\apache-tomcat-7.0.37\webapps\examples
    Apr 17, 2019 12:42:44 AM org.apache.catalina.core.ApplicationContext log
    INFO: ContextListener: contextInitialized()
    Apr 17, 2019 12:42:44 AM org.apache.catalina.core.ApplicationContext log
    INFO: SessionListener: contextInitialized()
    Apr 17, 2019 12:42:44 AM org.apache.catalina.core.ApplicationContext log
    INFO: ContextListener: attributeAdded(‘org.apache.jasper.compiler.TldLocationsCache’, ‘org.apache.jasper.compiler.TldLocationsCache@13f5d87’)
    Apr 17, 2019 12:42:44 AM org.apache.catalina.startup.HostConfig deployDirectory
    INFO: Deploying web application directory F:\Setup\eclipse_new\tomcat\apache-tomcat-7.0.37\webapps\host-manager
    Apr 17, 2019 12:42:45 AM org.apache.catalina.startup.HostConfig deployDirectory
    INFO: Deploying web application directory F:\Setup\eclipse_new\tomcat\apache-tomcat-7.0.37\webapps\manager
    Apr 17, 2019 12:42:45 AM org.apache.catalina.startup.HostConfig deployDirectory
    INFO: Deploying web application directory F:\Setup\eclipse_new\tomcat\apache-tomcat-7.0.37\webapps\ROOT
    Apr 17, 2019 12:42:45 AM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler [“http-bio-8080”]
    Apr 17, 2019 12:42:45 AM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler [“ajp-bio-8009”]
    Apr 17, 2019 12:42:45 AM org.apache.catalina.startup.Catalina start
    INFO: Server startup in 6656 ms
    Apr 17, 2019 12:46:55 AM org.apache.catalina.loader.WebappClassLoader modified
    SEVERE: Resource ‘/WEB-INF/lib/spring-context-4.3.9.RELEASE.jar’ is missing
    Apr 17, 2019 12:46:55 AM org.apache.catalina.core.StandardContext reload
    INFO: Reloading Context with name [/spring-web-example] has started
    Apr 17, 2019 12:47:00 AM org.apache.catalina.core.ApplicationContext log
    INFO: No Spring WebApplicationInitializer types detected on classpath
    Apr 17, 2019 12:47:00 AM org.apache.catalina.core.ApplicationContext log
    INFO: Initializing Spring FrameworkServlet ‘spring’
    Apr 17, 2019 12:47:00 AM org.springframework.web.servlet.DispatcherServlet initServletBean
    INFO: FrameworkServlet ‘spring’: initialization started
    Apr 17, 2019 12:47:00 AM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
    INFO: Refreshing WebApplicationContext for namespace ‘spring-servlet’: startup date [Wed Apr 17 00:47:00 IST 2019]; root of context hierarchy
    Apr 17, 2019 12:47:00 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-servlet.xml]
    Apr 17, 2019 12:47:01 AM org.apache.catalina.core.ApplicationContext log
    SEVERE: StandardWrapper.Throwable
    java.lang.IncompatibleClassChangeError: class org.springframework.core.type.classreading.ClassMetadataReadingVisitor has interface org.springframework.asm.ClassVisitor as super class
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2888)
    at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1172)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1558)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2888)
    at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1172)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1558)
    at org.springframework.core.type.classreading.SimpleMetadataReader.(SimpleMetadataReader.java:63)
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:98)
    at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:102)
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:93)
    at org.springframework.context.annotation.ConfigurationClassUtils.checkConfigurationClassCandidate(ConfigurationClassUtils.java:69)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:216)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:178)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:617)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:446)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5033)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5317)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3926)
    at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:426)
    at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1345)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1530)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1519)
    at java.lang.Thread.run(Unknown Source)

    Apr 17, 2019 12:47:01 AM org.apache.catalina.core.StandardContext loadOnStartup
    SEVERE: Servlet /spring-web-example threw load() exception
    java.lang.IncompatibleClassChangeError: class org.springframework.core.type.classreading.ClassMetadataReadingVisitor has interface org.springframework.asm.ClassVisitor as super class
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2888)
    at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1172)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1558)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2888)
    at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1172)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1558)
    at org.springframework.core.type.classreading.SimpleMetadataReader.(SimpleMetadataReader.java:63)
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:98)
    at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:102)
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:93)
    at org.springframework.context.annotation.ConfigurationClassUtils.checkConfigurationClassCandidate(ConfigurationClassUtils.java:69)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:216)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:178)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:617)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:446)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5033)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5317)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3926)
    at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:426)
    at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1345)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1530)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1519)
    at java.lang.Thread.run(Unknown Source)

    Apr 17, 2019 12:47:01 AM org.apache.catalina.core.StandardContext reload
    INFO: Reloading Context with name [/spring-web-example] is completed
    Apr 17, 2019 12:54:21 AM org.apache.catalina.loader.WebappClassLoader modified
    SEVERE: Resource ‘/WEB-INF/lib/commons-logging-1.2.jar’ is missing
    Apr 17, 2019 12:54:21 AM org.apache.catalina.core.StandardContext reload
    INFO: Reloading Context with name [/spring-web-example] has started
    Apr 17, 2019 12:54:23 AM org.apache.catalina.core.ApplicationContext log
    INFO: Marking servlet spring as unavailable
    Apr 17, 2019 12:54:24 AM org.apache.catalina.core.StandardContext loadOnStartup
    SEVERE: Servlet /spring-web-example threw load() exception
    java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1713)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1558)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:527)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:509)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:137)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5033)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5317)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3926)
    at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:426)
    at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1345)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1530)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1519)
    at java.lang.Thread.run(Unknown Source)

    Apr 17, 2019 12:54:24 AM org.apache.catalina.core.StandardContext reload
    INFO: Reloading Context with name [/spring-web-example] is completed
    Apr 17, 2019 12:56:14 AM org.apache.catalina.loader.WebappClassLoader modified
    INFO: Additional JARs have been added
    Apr 17, 2019 12:56:14 AM org.apache.catalina.core.StandardContext reload
    INFO: Reloading Context with name [/spring-web-example] has started
    Apr 17, 2019 12:56:18 AM org.apache.catalina.core.ApplicationContext log
    INFO: No Spring WebApplicationInitializer types detected on classpath
    Apr 17, 2019 12:56:18 AM org.apache.catalina.core.ApplicationContext log
    INFO: Initializing Spring FrameworkServlet ‘spring’
    Apr 17, 2019 12:56:18 AM org.springframework.web.servlet.DispatcherServlet initServletBean
    INFO: FrameworkServlet ‘spring’: initialization started
    Apr 17, 2019 12:56:19 AM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
    INFO: Refreshing WebApplicationContext for namespace ‘spring-servlet’: startup date [Wed Apr 17 00:56:19 IST 2019]; root of context hierarchy
    Apr 17, 2019 12:56:19 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-servlet.xml]
    Apr 17, 2019 12:56:21 AM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
    INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace ‘spring-servlet’: startup date [Wed Apr 17 00:56:19 IST 2019]; root of context hierarchy
    Apr 17, 2019 12:56:21 AM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
    INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace ‘spring-servlet’: startup date [Wed Apr 17 00:56:19 IST 2019]; root of context hierarchy
    Apr 17, 2019 12:56:22 AM org.springframework.web.servlet.DispatcherServlet initServletBean
    INFO: FrameworkServlet ‘spring’: initialization completed in 3113 ms
    Apr 17, 2019 12:56:22 AM org.apache.catalina.core.StandardContext reload
    INFO: Reloading Context with name [/spring-web-example] is completed

  2. subesh says:

    I browser i am getting Not Found Error and in the log getting error :

    [org.springframework.web.servlet.PageNotFound] (default task-40) No mapping found for HTTP request with URI [/ApiMgmt/] in DispatcherServlet with name ‘spring’

  3. Tarun says:

    I am getting an error
    No mapping found for HTTP request with URI [/spring-mvc/WEB-INF/views/home.jsp] in DispatcherServlet with name ‘spring’

  4. Valentina says:

    Hi Ishwarya, check the project name is correctly given.Because Pankaj used name spring-mvc-example. But your project name is different. Or else try tomcat 7 and jdk 1.8

  5. masood azhar says:

    ohh that nice

  6. RV says:

    I am getting the following error :
    Type Status Report

    Message /project1/WEB-INF/views/home.jsp

    Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

    Apache Tomcat/8.5.32
    Please help

  7. RV says:

    Hi Pankaj, great tutorial.
    But I’m getting the following error on the webpage :

    HTTP Status 404 – Not Found

    Type Status Report

    Message /project1/

    Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

    Apache Tomcat/8.5.32

    Please help

  8. RAju says:

    plzz add spring mvc jars url

    1. ArunSingh says:

      org.springframework
      spring-webmvc
      5.0.8.RELEASE

  9. Suresh Kumar says:

    I am getting an error :
    when i submit the name via home.jsp file then path is executed from root. Have checked all possibilities like setting /user,user and spring-mvc-exp/user in attribute section of form tag. Latter one gets executed successfully. Could not find out why hitting submit button from home jsp file, path is not relative. its taking frpm ip”port/user.

  10. Naveen says:

    WARNING: No mapping found for HTTP request with URI [/Myapp/] in DispatcherServlet with name ‘dispatcher’.

    what might be the error?

  11. kunal says:

    HTTP Status 500 – Servlet.init() for servlet spring threw exception

  12. shanth043 says:

    how to add password tab in above Example

  13. Arun says:

    Hi,

    What is the use of @Validated annotation here?

    Thanks.

  14. Dinesh Singh says:

    Thanks…
    It’s really very helpful post for beginners.

  15. meenakshi says:

    i have try to run this project many time but i m getting this error..
    i did get any solution , sir please help me to solve his error,
    i have imoprt your project after download.
    WARNING: No mapping found for HTTP request with URI [/spring-mvc-example/] in DispatcherServlet with name ‘spring’

    1. Dinesh Singh says:

      Please keep your home.jsp and user.jsp in view folder

  16. Rajendra says:

    Hi Pankaj, this is a nice artical. Thanks for sharing.

  17. mj says:

    Hello,

    I am getting multiple markers:
    requestMethod can not be resolved to a variable
    requestMapping can not to be resolved to a type

    any idea? Thanks.

    mj

  18. sathish.d says:

    how to import you download project in eclipse

    1. Pankaj says:

      Import as existing maven project.

  19. ishwarya says:

    Mar 22, 2018 10:41:27 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Server version: Apache Tomcat/9.0.5
    Mar 22, 2018 10:41:27 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Server built: Feb 6 2018 21:42:23 UTC
    Mar 22, 2018 10:41:27 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Server number: 9.0.5.0
    Mar 22, 2018 10:41:27 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: OS Name: Windows 10
    Mar 22, 2018 10:41:27 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: OS Version: 10.0
    Mar 22, 2018 10:41:27 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Architecture: amd64
    Mar 22, 2018 10:41:27 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Java Home: C:\Program Files\Java\jre-9.0.4
    Mar 22, 2018 10:41:27 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: JVM Version: 9.0.4+11
    Mar 22, 2018 10:41:27 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: JVM Vendor: Oracle Corporation
    Mar 22, 2018 10:41:27 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: CATALINA_BASE: C:\Users\ishwa\eclipse\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
    Mar 22, 2018 10:41:27 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: CATALINA_HOME: C:\Users\ishwa\Downloads\apache-tomcat-9.0.5-windows-x64\apache-tomcat-9.0.5
    Mar 22, 2018 10:41:27 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Command line argument: -Dcatalina.base=C:\Users\ishwa\eclipse\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
    Mar 22, 2018 10:41:27 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Command line argument: -Dcatalina.home=C:\Users\ishwa\Downloads\apache-tomcat-9.0.5-windows-x64\apache-tomcat-9.0.5
    Mar 22, 2018 10:41:27 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Command line argument: -Dwtp.deploy=C:\Users\ishwa\eclipse\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
    Mar 22, 2018 10:41:27 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Command line argument: -Dfile.encoding=Cp1252
    Mar 22, 2018 10:41:27 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
    INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jre-9.0.4\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\oraclexe\app\oracle\product\11.2.0\server\bin;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd;C:\Program Files\nodejs\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Java\jdk-9.0.1\bin;C:\Users\ishwa\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Microsoft VS Code\bin;C:\Users\ishwa\AppData\Roaming\npm;.]
    Mar 22, 2018 10:41:27 AM org.apache.coyote.AbstractProtocol init
    INFO: Initializing ProtocolHandler [“http-nio-8091”]
    Mar 22, 2018 10:41:27 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
    INFO: Using a shared selector for servlet write/read
    Mar 22, 2018 10:41:27 AM org.apache.coyote.AbstractProtocol init
    INFO: Initializing ProtocolHandler [“ajp-nio-8009”]
    Mar 22, 2018 10:41:27 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
    INFO: Using a shared selector for servlet write/read
    Mar 22, 2018 10:41:27 AM org.apache.catalina.startup.Catalina load
    INFO: Initialization processed in 1082 ms
    Mar 22, 2018 10:41:27 AM org.apache.catalina.core.StandardService startInternal
    INFO: Starting service [Catalina]
    Mar 22, 2018 10:41:27 AM org.apache.catalina.core.StandardEngine startInternal
    INFO: Starting Servlet Engine: Apache Tomcat/9.0.5
    Mar 22, 2018 10:41:28 AM org.apache.jasper.servlet.TldScanner scanJars
    INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    Mar 22, 2018 10:41:28 AM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
    WARNING: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [109] milliseconds.
    Mar 22, 2018 10:41:28 AM org.apache.catalina.startup.HostConfig deployDescriptor
    INFO: Deploying configuration descriptor [C:\Users\ishwa\eclipse\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\Catalina\localhost\SpringsMVC.xml]
    Mar 22, 2018 10:41:28 AM org.apache.catalina.startup.SetContextPropertiesRule begin
    WARNING: [SetContextPropertiesRule]{Context} Setting property ‘source’ to ‘org.eclipse.jst.jee.server:SpringsMVC’ did not find a matching property.
    Mar 22, 2018 10:41:30 AM org.apache.jasper.servlet.TldScanner scanJars
    INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    Mar 22, 2018 10:41:30 AM org.apache.catalina.core.ApplicationContext log
    INFO: No Spring WebApplicationInitializer types detected on classpath
    Mar 22, 2018 10:41:30 AM org.apache.catalina.core.ApplicationContext log
    INFO: Initializing Spring FrameworkServlet ‘spring’
    Mar 22, 2018 10:41:30 AM org.springframework.web.servlet.DispatcherServlet initServletBean
    INFO: FrameworkServlet ‘spring’: initialization started
    Mar 22, 2018 10:41:30 AM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
    INFO: Refreshing WebApplicationContext for namespace ‘spring-servlet’: startup date [Thu Mar 22 10:41:30 EDT 2018]; root of context hierarchy
    Mar 22, 2018 10:41:30 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-servlet.xml]
    Mar 22, 2018 10:41:32 AM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register
    INFO: Mapped “{[/user],methods=[POST]}” onto public java.lang.String com.journaldev.spring.controller.HomeController.user(com.journaldev.spring.model.User,org.springframework.ui.Model)
    Mar 22, 2018 10:41:32 AM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register
    INFO: Mapped “{[/],methods=[GET]}” onto public java.lang.String com.journaldev.spring.controller.HomeController.home(java.util.Locale,org.springframework.ui.Model)
    Mar 22, 2018 10:41:32 AM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
    INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace ‘spring-servlet’: startup date [Thu Mar 22 10:41:30 EDT 2018]; root of context hierarchy
    Mar 22, 2018 10:41:32 AM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
    INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace ‘spring-servlet’: startup date [Thu Mar 22 10:41:30 EDT 2018]; root of context hierarchy
    Mar 22, 2018 10:41:32 AM org.springframework.web.servlet.DispatcherServlet initServletBean
    INFO: FrameworkServlet ‘spring’: initialization completed in 2164 ms
    Mar 22, 2018 10:41:32 AM org.apache.catalina.startup.HostConfig deployDescriptor
    INFO: Deployment of configuration descriptor [C:\Users\ishwa\eclipse\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\Catalina\localhost\SpringsMVC.xml] has finished in [4,243] ms
    Mar 22, 2018 10:41:32 AM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler [“http-nio-8091”]
    Mar 22, 2018 10:41:32 AM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler [“ajp-nio-8009”]
    Mar 22, 2018 10:41:32 AM org.apache.catalina.startup.Catalina start
    INFO: Server startup in 4971 ms
    Home Page Requested, locale = en_US

  20. ishwarya says:

    I tried with many projects but i am getting the same error can you please help me with this?

    WARNING: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [109] milliseconds.
    WARNING: [SetContextPropertiesRule]{Context} Setting property ‘source’ to ‘org.eclipse.jst.jee.server:SpringsMVC’ did not find a matching property.

    1. Rajendra says:

      Hi Ishwarya, check your pom.xml has both the spring-web and spring-webmvc included with same spring release.

  21. Arulselvan says:

    i am getting this type of error what i do?

    type Exception report

    message Servlet.init() for servlet spring threw exception

    description The server encountered an internal error that prevented it from fulfilling this request.

    exception

    javax.servlet.ServletException: Servlet.init() for servlet spring threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:748)

    root cause

    org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/spring-servlet.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/spring-servlet.xml]
    org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344)
    org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
    org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
    org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
    org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
    org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
    org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
    org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
    org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:614)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:515)
    org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:171)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:748)

    root cause

    java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/spring-servlet.xml]
    org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141)
    org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)
    org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
    org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
    org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
    org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
    org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
    org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
    org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
    org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:614)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:515)
    org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:171)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:748)

    note The full stack trace of the root cause is available in the Apache Tomcat/8.0.47 logs.

    1. Pankaj says:

      java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/spring-servlet.xml]

      Check if file is packaged in the WAR or not.

  22. manjunatha says:

    its asking for username and password
    and localhost8080 is not working

  23. siddhartha Jain says:

    Hi ,
    Getting exception org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/KATFIN/] in DispatcherServlet with name ‘spring’

  24. Baburam Neupane says:

    user.getUserName()

    when is user.setUserName() called . how the value is set?

    1. Pankaj says:

      That is taken care by Spring MVC framework.

      1. Sam says:

        where the user object is created?

    2. mj says:

      seems it is set in this code in the model (POJO) class.

      public void setUserName(String userName) {
      this.userName = userName;

      Thank you. Very useful tutorial.

  25. Udit says:

    I am getting Http:404 Error. please help.

    Thanks.

    1. Amit says:

      Please check context value in log.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages