Spring 3 MVC: Create Hello World application in Spring 3.0 MVC

Welcome to the Part 2 of Spring 3.0 MVC Series. In previous article we went through the Introduction of Spring MVC 3.0 framework, its request processing lifecycle and architecture diagram. In this article, let us create a simple Hello World application in Spring MVC 3.0.


For creating the hello world demo application, we will use Eclipse IDE.

Things We Need

Before we starts with our first Hello World Struts 2 Example, we will need few tools.

  1. JDK 1.5 above (download)
  2. Tomcat 5.x above or any other container (Glassfish, JBoss, Websphere, Weblogic etc) (download)
  3. Eclipse 3.2.x above (download)
  4. Spring 3.0 MVC JAR files:(download). Following are the list of JAR files required for this application.
    • commons-logging-1.0.4.jar
    • jstl-1.2.jar
    • org.springframework.asm-3.0.1.RELEASE-A.jar
    • org.springframework.beans-3.0.1.RELEASE-A.jar
    • org.springframework.context-3.0.1.RELEASE-A.jar
    • org.springframework.core-3.0.1.RELEASE-A.jar
    • org.springframework.expression-3.0.1.RELEASE-A.jar
    • org.springframework.web.servlet-3.0.1.RELEASE-A.jar
    • org.springframework.web-3.0.1.RELEASE-A.jar

Note that depending on the current version of Spring MVC, the version number of above jar files may change.

Our Goal

Our goal is to create a basic Spring MVC application using latest 3.0 version. There will be an index page which will display a link “Say Hello” to user. On clicking this link, user will be redirected to another page hello which will display a message “Hello World, Spring 3.0!”.
spring-mvc-hello-world-screen

Getting Started

Let us start with our first Spring 3.0 MVC based application.
Open Eclipse and goto File -> New -> Project and select Dynamic Web Project in the New Project wizard screen.
Dynamic Web Project in Eclipse

After selecting Dynamic Web Project, press Next.

eclipse-dynamic-web-project

Write the name of the project. For example Spring3MVC. Once this is done, select the target runtime environment (e.g. Apache Tomcat v6.0). This is to run the project inside Eclipse environment. After this press Finish.

Once the project is created, you can see its structure in Project Explorer.
spring-mvc-3-eclipse-project
Now copy all the required JAR files in WebContent > WEB-INF > lib folder. Create this folder if it does not exists.
spring-3-mvc-jar-files

The Spring Controller Class

We will need a spring mvc controller class that will process the request and display a “Hello World” message. For this we will create a package net.viralpatel.spring3.controller in the source folder. This package will contain the Controller file.
spring-3-package

Create a class called HelloWorldController in net.viralpatel.spring3.controller package and copy following content into it.

File: net.viralpatel.spring3.controller.HelloWorldController

package net.viralpatel.spring3.controller;

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

@Controller
public class HelloWorldController {

	@RequestMapping("/hello")
	public ModelAndView helloWorld() {

		String message = "Hello World, Spring 3.0!";
		return new ModelAndView("hello", "message", message);
	}
}

Note that we have annotated the HelloWorldController class with @Controller and @RequestMapping("/hello") on line 7 and 10. When Spring scans our package, it will recognize this bean as being a Controller bean for processing requests. The @RequestMapping annotation tells Spring that this Controller should process all requests beginning with /hello in the URL path. That includes /hello/* and /hello.html.

The helloWorld() method returns ModelAndView object. The ModelAndView object tries to resolve to a view named “hello” and the data model is being passed back to the browser so we can access the data within the JSP. The logical view name will resolve to "/WEB-INF/jsp/hello.jsp". We will discuss this shortly how the logical name “hello” which is return in ModelAndView object is mapped to path /WEB-INF/jsp/hello.jsp.

The ModelAndView object also contains a message with key “message” and value “Hello World, Spring 3.0!”. This is the data that we are passing to our view. Normally this will be a value object in form of java bean that will contain the data to be displayed on our view. Here we are simply passing a string.

The View: Create JSP

To display the hello world message we will create a JSP. Note that this JSP is created in folder /WEB-INF/jsp. Create hello.jsp under WEB-INF/jsp directory and copy following content into it.

File: WEB-INF/jsp/hello.jsp

<html>
<head>
	<title>Spring 3.0 MVC Series: Hello World - ViralPatel.net</title>
</head>
<body>
	${message}
</body>
</html>

The above JSP simply display a message using expression ${message}. Note that the name “message” is the one which we have set in ModelAndView object with the message string.

Also we will need an index.jsp file which will be the entry point of our application. Create a file index.jsp under WebContent folder in your project and copy following content into it.

File: WebContent/index.jsp

<html>
<head>
	<title>Spring 3.0 MVC Series: Index - ViralPatel.net</title>
</head>
<body>
	<a href="hello.html">Say Hello</a>
</body>
</html>

Mapping Spring MVC in WEB.xml

As discussed in the previous article (Introduction to Spring 3.0 MVC), the entry point of Spring MVC application will be the Servlet define in deployment descriptor (web.xml). Hence we will define an entry of org.springframework.web.servlet.DispatcherServlet class in web.xml.
Open web.xml file which is under WEB-INF folder and copy paste following code.

File: WEB-INF/web.xml

<?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"
	xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>Spring3MVC</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>spring</servlet-name>
		<url-pattern>*.html</url-pattern>
	</servlet-mapping>
</web-app>

The above code in web.xml will map DispatcherServlet with url pattern *.html. Also note that we have define index.jsp as welcome file.

One thing to note here is the name of servlet in <servlet-name> tag in web.xml. Once the DispatcherServlet is initialized, it will looks for a file name [servlet-name]-servlet.xml in WEB-INF folder of web application. In this example, the framework will look for file called spring-servlet.xml.

Spring configuration file

Create a file spring-servlet.xml in WEB-INF folder and copy following content into it.

File: WEB-INF/spring-servlet.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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	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="net.viralpatel.spring3.controller" />

	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.UrlBasedViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

In the above xml configuration file, we have defined a tag <context:component-scan>. This will allow Spring to load all the components from package net.viralpatel.spring3.controller and all its child packages. This will load our HelloWorldController class. Also we have defined a bean viewResolver. This bean will resolve the view and add prefix string /WEB-INF/jsp/ and suffix .jsp to the view in ModelAndView. Note that in our HelloWorldController class, we have return a ModelAndView object with view name “hello”. This will be resolved to path /WEB-INF/jsp/hello.jsp.

That’s All Folks

You may want to run the application now and see the result. I assume you have already configured Tomcat in eclipse. All you need to do:
Open Server view from Windows > Show View > Server. Right click in this view and select New > Server and add your server details.
To run the project, right click on Project name from Project Explorer and select Run as > Run on Server (Shortcut: Alt+Shift+X, R)
spring-mvc-hello-world-screen

Download Source Code

Click here to download source code (9.05kb)

Moving On

In this tutorial we created a small Hello World application using Spring 3.0 MVC framework. Also we learned about the spring configuration and different annotations like @Controller and @RequestMapping. In next article we will see how easy it is to handle form data using Spring 3.0 MVC.



194 Comments

  • 404 problem wrote on 22 February, 2011, 18:35

    why i still figure out 404 problem :( ( it cause by servlet spring is not available :( ( anyone help me please…

    • Sudhir Nayak wrote on 22 March, 2011, 11:41

      You Miss Some lib or check the deploye

  • Kailash Yadav wrote on 2 March, 2011, 16:14

    404 Problem :

    Check in your WEB-INF/spring-servlet.xml
    Make sure your base package matches the package your controller is in.

  • Nest Chen wrote on 4 March, 2011, 16:40

    The “hello()” in line with the content, ” The hello() method returns ModelAndView object ……” should be replaced with “helloWorld()” , shouldn’t that ?

    • Viral Patel wrote on 4 March, 2011, 17:50

      @Nest Chen – well catch :) I have update the typo and changed it to helloWorld().

  • Nest Chen wrote on 4 March, 2011, 17:15

    I got the same problem with 404 error.
    I am sure that base-package in spring-servlet.xml is corresponding to my package name where my controller is in …

    • Viral Patel wrote on 4 March, 2011, 17:51

      What URL are you trying to access in Browser? Also are there any errors in the server logs?

      • Chris James wrote on 7 March, 2011, 21:34

        I am getting the same problem. I am running the code in Jetty rather than tomcat, but that should’nt make a difference should it?

        Here is the error i get when I try to go to the hello.html

        WARNING: No mapping found for HTTP request with URI [/webfrontend/hello.html] in
        DispatcherServlet with name ‘spring’

        I get a normal 404 if i just try going to hello.html, so i dont think having “webfrontend” is the problem

        • Viral Patel wrote on 7 March, 2011, 22:04

          @Chris – The server shouldn’t be a problem. Can you check if your server log shows some exception on starting or loading the app? There must be something wrong the way it loaded the application. It seems that the classes are not getting loaded and thus giving 404 error.

        • Irene wrote on 12 April, 2011, 14:03

          I had the same problem with you, and I fixed it by rebuilding the project. Hope it helps.

  • Nest Chen wrote on 4 March, 2011, 22:15

    In the example you providded , it shows 404 error after clicking “Say hello” superlink…
    Another strange thing is that my web.xml and spring-servlet.xml in my project have the same contents as yours…but it shows 404 error when running on Tomcat 7, in the other word, it should show index.jsp but it didn’t … but it did when running with the project you providded…
    Certainly , had been set in web.xml…

  • Nest Chen wrote on 4 March, 2011, 22:16

    <welcome-file-list> loses in the last line …..

  • SA wrote on 19 March, 2011, 7:43

    404 error (Servlet spring is not available)? I have fixed it by adding commons-logging-1.0.4 in the classpath.
    Cause: Failing to load org.springframework.web.servlet.DispatcherServlet with classnotfound exception and can’t scan/load the spring servlet at runtime.

  • Fabiano wrote on 30 March, 2011, 19:16

    Estou comecando agora, mas pela lógica que peguei, falta um bean no spring-servlet

    tambem estava tendo o 404

  • press0 wrote on 3 April, 2011, 9:02

    Viral – Works great in Tomcat. Thanks

  • Gautham Nookala wrote on 25 April, 2011, 19:59

    I am using tomcat 6. I had to copy the JSTL 1.2 jar files to the tomcat lib directory. Please see the following link.
    http://www.mularien.com/blog/2008/02/19/tutorial-how-to-set-up-tomcat-6-to-work-with-jstl-12/comment-page-1/#comment-46628

  • Robert wrote on 4 May, 2011, 0:32

    Tip: be careful to use jstl-1.2.jar instead of the JSTL library that comes with the Spring framework distribution. The one that comes with Spring is version 1.1.2 and will lead to errors in your JSPs. Learned that the hard way…

  • Naveen SInha wrote on 4 May, 2011, 12:34

    Simple replace *.html with / in web.xml

  • sri wrote on 4 May, 2011, 16:35

    Hi
    I am deploying the application on Spring Source Tool Suite.
    Its showing that the following error
    SEVERE: Error loading WebappClassLoader
    context: /SpringInAction
    delegate: false
    repositories:
    /WEB-INF/classes/
    ———-> Parent Classloader:
    org.apache.catalina.loader.StandardClassLoader@6e70c7
    org.springframework.web.servlet.DispatcherServlet
    java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1647)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1493)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1095)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:993)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4387)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4700)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
    at org.apache.catalina.core.StandardService.start(StandardService.java:525)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:701)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:585)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
    May 4, 2011 4:58:43 PM org.apache.catalina.core.StandardContext loadOnStartup
    SEVERE: Servlet /SpringInAction threw load() exception
    java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1647)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1493)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1095)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:993)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4387)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4700)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
    at org.apache.catalina.core.StandardService.start(StandardService.java:525)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:701)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:585)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
    May 4, 2011 4:58:43 PM org.apache.coyote.http11.Http11Protocol start
    INFO: Starting Coyote HTTP/1.1 on http-8080
    May 4, 2011 4:58:44 PM org.apache.jk.common.ChannelSocket init
    INFO: JK: ajp13 listening on /0.0.0.0:8009
    May 4, 2011 4:58:44 PM org.apache.jk.server.JkMain start
    INFO: Jk running ID=0 time=0/63 config=null
    May 4, 2011 4:58:44 PM org.apache.catalina.startup.Catalina start
    INFO: Server startup in 804 ms
    Please help me in solving this…

  • Frank Serkland wrote on 6 May, 2011, 5:37

    I am following this example and using the listed jars, but Eclipse is telling me that all three classes in HelloWorldController.java cannot be resolved. Any idea why?

  • Marco wrote on 18 May, 2011, 0:34

    All is correctly compiled, server starts but got a 404, any hint? I’m running Tomcat 6.

    • Marco wrote on 18 May, 2011, 0:53

      Ok, solved the first 404. Still got a 404 when clicking on the link, maybe is a problem with the servlet-mapping?thanks.

  • Skiff_ wrote on 22 May, 2011, 2:38

    22.05.2011 0:37:06 org.apache.jk.server.JkMain start
    INFO: Jk running ID=0 time=0/45 config=null
    22.05.2011 0:37:06 org.apache.catalina.startup.Catalina start
    INFO: Server startup in 1123 ms
    22.05.2011 0:37:13 org.apache.catalina.core.StandardWrapperValve invoke
    INFO: Servlet spring is currently unavailable

    Have a problem too.

  • Medloh wrote on 25 May, 2011, 5:44

    My link wasn’t working. Looks like it was because I was using the jstl.jar from Tomcat 7. I grabbed a copy of jstl-1.2.jar I found from googling to replace tomcat’s jstl.jar, and it works now.

  • pramod wrote on 30 May, 2011, 18:07

    have to add antlr-runtime.jar file to make this work.

  • venkadesh wrote on 4 June, 2011, 21:02

    Thanks for this. its really helpful.

    in web.xml servlet-mapping will be defined like this

    spring
    /

  • Anil wrote on 8 June, 2011, 12:02

    i changed web.xml, .html with /. but now i am geing below error

    SEVERE: Servlet.service() for servlet spring threw exception
    java.lang.NoSuchMethodError: org.springframework.web.bind.annotation.support.HandlerMethodResolver.(Ljava/lang/Class;)V
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodResolver.(AnnotationMethodHandlerAdapter.java:399)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.getMethodResolver(AnnotationMethodHandlerAdapter.java:389)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.supports(AnnotationMethodHandlerAdapter.java:286)
    at org.springframework.web.servlet.DispatcherServlet.getHandlerAdapter(DispatcherServlet.java:1096)
    at org.springframework.web.servlet.DispatcherServlet.getLastModified(DispatcherServlet.java:966)

  • zamboo wrote on 11 June, 2011, 1:27

    have a 404 problem. What is exactly the url for access ?

    • Zamboo wrote on 15 June, 2011, 20:39

      OK work fine… Had to reboot my jboss server…
      Thx for ur share.

  • Syevin wrote on 19 June, 2011, 7:17

    Hello,

    I am looking forward to starting this series of tutorials. After following this one, I am reluctant to say that I have also received a 404-Page Not Found error.

    I have installed JSTL 1.2 to Tomcat. I have changed the servlet mapping from *.html to ‘/’ and vice versa. I have followed all of the directions in your post explicitly, and have copied/pasted everything directly.

    Why isn’t this working? I am *not* getting an error message saying that my spring-servlet cannot be accessed, and I’ve read everyone else’s comments which haven’t helped. I really want to start developing with spring mvc and the learning curve is great – I just can’t get past this hangup. I will literally check this every day until you can reply, Viral.

    Thanks for the good work and please let me know asap.

    -Sam

    • Viral Patel wrote on 20 June, 2011, 11:57

      @Syevin, can you please see if there are any other exceptions in the error log. Also check if all the required JAR file dependencies are present.

      • Syevin wrote on 20 June, 2011, 20:01

        No additional errors in the error log, not that I could see anyways. And they are all present – I am managing dependencies with maven. I have spring3 (all), commons logging, etc. I installed jstl1.2 to tomcat, not to my project. When I tried adding jstl1.2.jar to my application, I kept getting nullpointerexceptions when I clicked the “say hello” link. I don’t get those errors when I have jstl in the tomcat lib folder – but I still get the 404 error when I click “Say Hello” (and everything is configured exactly as you have).

        • Syevin wrote on 20 June, 2011, 20:06

          What is your e-mail address? Can I send you my maven project so you can have a look yourself?

          • leniza wrote on 17 November, 2011, 12:25

            Could you decide the problem? i have the same..

  • Bazza wrote on 21 June, 2011, 18:30

    I couldn’t get this to work either, so realising that it wasn’t the code (too many people saying it works for that ;-) I tried physically building the WAR file and deploying to Weblogic10.3 rather than using the server started in Eclipse (I tried the tomcat, j2ee basic AND weblogic – none of these worked). Once the war file was deployed, I acccessed the index.jsp, clicked on the link and it worked fine. So the problem appears to be how the eclipse configured container “deploys” the app. Bit annoying that as it will make debugging issues a lot harder.

  • Syevin wrote on 22 June, 2011, 18:29

    I am now seeing this (using Maven for dependency management):

    Jun 22, 2011 9:19:14 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\Java\jdk1.6.0_26\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Windows\system32;C:\Windows;C:\Windows\system32\wbem;C:\Windows\system32\windowspowershell\v1.0\;c:\program files\thinkpad\bluetooth software\;c:\program files\thinkpad\bluetooth software\syswow64;c:\program files (x86)\intel\services\ipt\;c:\program files\intel\wifi\bin\;c:\program files\common files\intel\wirelesscommon\;c:\program files (x86)\ibm\gsk8\lib;C:\PROGRA~2\IBM\SQLLIB\BIN;C:\PROGRA~2\IBM\SQLLIB\FUNCTION;C:\PROGRA~2\IBM\SQLLIB\SAMPLES\REPL;C:\apache-maven-3.0.3\bin;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;.
    Jun 22, 2011 9:19:14 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
    WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ‘source’ to ‘org.eclipse.jst.j2ee.server:PDFinvoice’ did not find a matching property.
    Jun 22, 2011 9:19:14 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
    WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ‘source’ to ‘org.eclipse.jst.jee.server:Spring3MVC’ did not find a matching property.
    Jun 22, 2011 9:19:14 AM org.apache.coyote.http11.Http11Protocol init
    INFO: Initializing Coyote HTTP/1.1 on http-9090
    Jun 22, 2011 9:19:14 AM org.apache.catalina.startup.Catalina load
    INFO: Initialization processed in 1009 ms
    Jun 22, 2011 9:19:14 AM org.apache.catalina.core.StandardService start
    INFO: Starting service Catalina
    Jun 22, 2011 9:19:14 AM org.apache.catalina.core.StandardEngine start
    INFO: Starting Servlet Engine: Apache Tomcat/6.0.32
    log4j:WARN No appenders could be found for logger (org.springframework.web.servlet.DispatcherServlet).
    log4j:WARN Please initialize the log4j system properly.
    Jun 22, 2011 9:19:16 AM org.apache.catalina.core.ApplicationContext log
    INFO: Initializing Spring FrameworkServlet ‘spring’
    Jun 22, 2011 9:19:17 AM org.apache.catalina.core.ApplicationContext log
    INFO: Marking servlet spring as unavailable
    Jun 22, 2011 9:19:17 AM org.apache.catalina.core.ApplicationContext log
    SEVERE: Error loading WebappClassLoader
    context: /Spring3MVC
    delegate: false
    repositories:
    /WEB-INF/classes/
    ———-> Parent Classloader:
    org.apache.catalina.loader.StandardClassLoader@95c083
    org.springframework.web.servlet.DispatcherServlet
    java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1095)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:993)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4420)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4733)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
    at org.apache.catalina.core.StandardService.start(StandardService.java:525)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
    Jun 22, 2011 9:19:17 AM org.apache.catalina.core.StandardContext loadOnStartup
    SEVERE: Servlet /Spring3MVC threw load() exception
    java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1095)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:993)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4420)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4733)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
    at org.apache.catalina.core.StandardService.start(StandardService.java:525)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
    Jun 22, 2011 9:19:17 AM org.apache.coyote.http11.Http11Protocol start
    INFO: Starting Coyote HTTP/1.1 on http-9090
    Jun 22, 2011 9:19:17 AM org.apache.jk.common.ChannelSocket init
    INFO: JK: ajp13 listening on /0.0.0.0:9009
    Jun 22, 2011 9:19:17 AM org.apache.jk.server.JkMain start
    INFO: Jk running ID=0 time=0/18 config=null
    Jun 22, 2011 9:19:17 AM org.apache.catalina.startup.Catalina start
    INFO: Server startup in 2348 ms

  • Walter wrote on 5 July, 2011, 14:24

    Everything is working except that the message key is not rendered at all. It just shows ${message} instead of the value that belongs to the key. What is going wrong?

    • Kim wrote on 5 October, 2011, 1:11

      Walter,
      I get also the same result as you “${message}” in stead of the value. Have you found a solution? If so, could you help my please ?

  • Robert wrote on 7 July, 2011, 0:02

    Syevin, did you find a solution to your problem? I think I’m having the same issue.

  • AA wrote on 17 July, 2011, 6:53

    if you get 404 error when you click on say hello link then the problem is in WEB-INF/spring-servlet.xml.
    the author here did not show all the packages and folders clearly. So, in order to fix this you must have “HelloWorldController” class in net.viralpatel.spring3.controller or you can update you WEB-INF/spring-servlet.xml <context:component-scan
    base-package=
    path to where you "HelloWorldController" class actually lives.

  • leon wrote on 3 August, 2011, 7:43

    help me with this please. i have followed your steps correctly but it still doesnt run properly when launched on server. (there are no errors in the HelloWorldController class)

    Error
    cannot Deploy Spring
    Deployment Error for module: Spring: Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: java.util.ServiceConfigurationError: javax.servlet.ServletContainerInitializer: Provider org.springframework.web.SpringServletContainerInitializer could not be instantiated: java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory. Please see server.log for more details

  • Jiro wrote on 3 August, 2011, 7:51

    When creating a project from eclipse helios sr2, i already have a predefined .xml file in my WebContent/WEB-INF folder which is named sub-web.xml. Do i still need to create a new web.xml in that directory? Or can i just copy the codes of web.xml to it.

    • shen wrote on 4 August, 2011, 6:57

      You should create a new file named “web.xml”, or you should rename the file “subweb.xml” to “web.xml” .

  • sina wrote on 5 August, 2011, 6:47

    very good and easy to follow!thanks

  • Akhtar wrote on 5 August, 2011, 12:26

    Yeah…thanks i finally run it. first i was got 404 not found error. because of netbean create default dispatcher-servlet.xml. so delete it and recreate xml with spring-servlet.xml. after i was get jstl error. then i have added jstl library. Now its working very well.

  • Bolat wrote on 8 August, 2011, 4:15

    Hi,

    I have followed this example step-by-step and for some reason I am getting this message:

    08-Aug-2011 00:07:59 org.springframework.web.servlet.DispatcherServlet noHandlerFound
    WARNING: No mapping found for HTTP request with URI [/ViralPatel/hello] in DispatcherServlet with name ‘spring

    …And when I click on “Say Hello”, I get the following error message:

    HTTP Status 404 – The requested resource () is not available.

    I have even changed the web.xml to include / for all requests instead of *.html.

    My setup is: Springsource (Eclipse) IDE, Java 6, Tomcat 7 and Spring 3.0.5 release.

    Full IDE console dump is:

    08-Aug-2011 00:07:40 org.apache.catalina.core.AprLifecycleListener init
    WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ‘source’ to ‘org.eclipse.jst.jee.server:ViralPatel’ did not find a matching property.
    08-Aug-2011 00:07:41 org.apache.coyote.AbstractProtocol init
    INFO: Initializing ProtocolHandler ["http-apr-8080"]

    WARNING: No mapping found for HTTP request with URI [/ViralPatel/hello] in DispatcherServlet with name ‘spring’

    Many thanks,
    Bolat

  • Bolat wrote on 10 August, 2011, 18:09

    Opps, I made a similar mistake as Akhtar’s above. Hence, ALL those who are having 404 errors, do NOT use the IDE generated web.xml file, but rather download the web.xml from this tutorial and use this file directly as mistakes can happen while copy & pasting as we are all learing the powerful features of Spring. Another point: all the jars are NOT available in the download here, so make sure you google for the 2 missing jars, that is, commons-logging-1.0-4.jar and jstl-1.2.jar (note: you may have trouble finding jstl-1.2.jar, you can download it from the mvnrepository from the following link http://mvnrepository.com/artifact/javax.servlet/jstl/1.2 ).

  • Ivo wrote on 11 August, 2011, 16:43

    I tried it example. It works, but if i click the “Say Hello”-link, i get an message:

    TTP Status 404 – /Spring3MVC/hello
    type Status report
    message /Spring3MVC/hello
    description The requested resource (/Spring3MVC/hello) is not available.

    Can anybody help me?

  • Leon wrote on 15 August, 2011, 8:35

    i am having a hard time fixing the web.xml. in my project, i use glassfish 3 as a server. so i have a predefined web.xml which is named sun-web.xml. Everytime I copy the codes of your web.xml to my sun-web.xml it has an error. My project will also not run if i rename my sun-web.xml to web.xml.

    There is also an error when i copy your WEB-INF/spring-servlet.xml codes to my spring-servlet.xml. help me please. i am currently using STS 2.7.1

  • Elango wrote on 5 September, 2011, 10:37

    In the index.jsp the link should have the href=”hello.html” not “hello”

  • tom wrote on 17 September, 2011, 17:41

    If you still got a problem about 404 with not found Servlet, please google jstl-1.2.jar and commons-logging-1.0-4.jar, copy that into your lib folder and that definetly solve that problem.

  • Rodrigo wrote on 22 September, 2011, 22:36

    Finally I exported the project in a WAR file and copied it in the webapps folder of my Tomcat installation. I started Tomcat from .bat/.sh file and everything’s OK. Don’t use the embebed Tomcat in Eclipse. In fact it’s not the first time I have problem with Eclipse’s Tomcat. Strange thing is with another server (Jboss, Jetty, etc) I don’t have problems.

    Hope That Helps…

  • yash wrote on 27 September, 2011, 17:52

    thanx a lot………..really really great tutorial for a beginners…….keep up the good work……….

  • abou zakaria wrote on 2 October, 2011, 3:59

    well thank you very much for your effort !
    Actually i had the same prob as the others however i noticed that the code sorce to download get a mistake!
    have a look at the index.jsp and you ll notice that the href is targetting to hello and not hello.html
    and that is why we get the 404 message!
    thanks again!!

  • Dattatray Shinde wrote on 5 October, 2011, 11:33

    I had set up the application as per the steps stated above.But it was throwing error for some class in the jar “org.apache.commons.logging “.Then I add jar “commons-logging-1.1.1.jar” and now it’s working fine.
    Download link : http://tomcat.apache.org/download-60.cgi.

    Hope this will help!!!

  • Raja wrote on 18 October, 2011, 14:28

    Thanks

  • Tulip wrote on 20 October, 2011, 3:26

    Hi Viral,
    I tried your application step by step, as you asked in the way to do. But after clicking in the link in the index page, it is giving me blank “hello.jsp” page (Message isnt displaying).

    Pls suggest wat should i do now.

    Appreciate your quick response.

    • Krishna wrote on 20 October, 2011, 17:19

      For me too, similar problem…I am getting “$message” instead of “Hello Eorld Spring 3.0!!” Any guidance would be appreciated.

      Thanks.

  • TL wrote on 24 October, 2011, 8:38

    To make this work, you need all jars specified in this tutorial (for safety, get all spring jar files)
    The structure of the website should be
    WEB-INF
    —— classes
    ———– java classes
    —— jsp
    ———– all jsp files
    —— lib
    ———- all jar files mentioned above
    index.jsp

    all you need is to copy and paste the source codes mentioned above. I am using TomCat 7.0 and this works for me without any extra configuration. If you have error (404), check the server log and it will tell you what the problems are.
    Hope this helps

  • Nitin wrote on 30 October, 2011, 15:23

    its really good tutorial. Viral Patel u rock ..Thanks
    -Nitin

  • Ram wrote on 1 November, 2011, 6:03

    It seems none of the above suggestions are completely solving 404 issue, there could be some other unknown issues. For me it worked perfectly fine, when played with plain lib files and failed when I added maven dependencies…. Any issues with dispatcher servlet ? don’t know….

    Also noted that spring config file seems looking for …. does it need it…

    Its a pretty shame that there is no validation support for a given web.xml and spring-servlet.xml files…

    Hope things will improve in future for Spring…

  • Elliot wrote on 5 November, 2011, 1:00

    The problem is that the web.xml defines a url-pattern of “*.html” but the controller request mapping (@RequestMapping) defines the mapping as “/hello” and does not include the necessary .html suffix.

    To fix this issue, simply either change the @RequestMapping to “/hello.html” or change the web.xml URL pattern to “/”.

  • shahzeb wrote on 17 November, 2011, 15:26

    Nice tutorial

  • lucky-liu wrote on 18 November, 2011, 11:03

    thanks. i want see,XXX-servlet.xml can be replaced by bean.xml? thank you.

    • Viral Patel wrote on 18 November, 2011, 15:03

      @lucky-liu: You can use DispatcherServlet’s init-param “contextConfigLocation” to override the default context filename and add your own bean.xml. Refer this article for more details – Change spring-servlet.xml filename

  • vamsh wrote on 22 November, 2011, 15:01

    Hi,
    am getting 404 error:Servlet spring is not available…..getting say hello link but if i click on that link am getting the above error….plz do help ….

    • Viral Patel wrote on 22 November, 2011, 16:01

      @Vamsh – Please check your server error log (console). There must be some exception there.

Leave a Reply

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

*