Spring MVC hello world annotation example

Spring 3
This tutorial is using Spring 2.5.6, you may interest at this Spring 3 MVC hello world example.

In this tutorial, we show you how to create a Spring @MVC annotation-based hello world example.

Technologies used :

  1. Spring 2.5.6
  2. JDK 1.6
  3. Maven 3
  4. Eclipse 3.6

P.S This annotation-based example is converted from last Spring MVC hello world XML-based example.

1. Spring Dependency

Spring @MVC is bundled in the same spring-webmvc.jar. Declares following dependencies in your Maven pom.xml file.

	<!-- Spring framework --> 
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring</artifactId>
		<version>2.5.6</version>
	</dependency>
 
       <!-- Spring MVC framework --> 
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
		<version>2.5.6</version>
	</dependency>

2. Controller & Handler Mapping

Now, you can use @Controller and @RequestMapping to replace the XML configuration.

  1. Controller – The controller class is no longer need to extends base controller like AbstractController or SimpleFormController, just simply annotate the class with a @Controller annotation.
  2. Handler Mapping – No more declaration for the handler mapping like BeanNameUrlHandlerMapping, ControllerClassNameHandlerMapping or SimpleUrlHandlerMapping, all are replaced with a standard @RequestMapping annotation.

File : HelloWorldController.java

package com.mkyong.common.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
@RequestMapping("/welcome")
public class HelloWorldController{
 
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView helloWorld(){
 
		ModelAndView model = new ModelAndView("HelloWorldPage");
		model.addObject("msg", "hello world");
 
		return model;
	}
}

If the @RequestMapping is applied at class level (can apply at method level with multi-actions controller), it required to put a RequestMethod to indicate which method to handle the mapping request.

In this case, if an URI pattern “/welcome” is requested, it will map to this HelloWorldController, and handle the request with helloWorld() method.

3. View Resolver

Sadly, you are still required to configure the view resolver.

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

4. View

A simple JSP for demonstration.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
	<h1>Spring MVC Hello World Annotation Example</h1>
 
	<h2>${msg}</h2>
 
</body>
</html>

5. Components Auto Scanning

To make Spring @MVC annotation work, you have to enable Spring’s auto component scanning feature through the <context:component-scan> element.

<beans ...
   xmlns:context="http://www.springframework.org/schema/context
   ...
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 
	<context:component-scan base-package="com.mkyong.common.controller" />
 
</beans>
Note
You may interest at this Spring auto scanning components article.

Spring Configuration
See a complete Spring configuration file.

File : mvc-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 
	<context:component-scan base-package="com.mkyong.common.controller" />
 
	<bean id="viewResolver"
      	   class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
           <property name="prefix">
              <value>/WEB-INF/pages/</value>
           </property>
           <property name="suffix">
              <value>.jsp</value>
           </property>
        </bean>
 
</beans>

6. web.xml

No exception, you are still required to configure the web.xml to enable the Spring MVC features.

File : web.xml

 ...
 <servlet>
  	<servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
  </servlet>
 
  <servlet-mapping>
 	<servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
  </servlet-mapping>
 
  <context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
  </context-param>
 
  <listener>
       <listener-class>
          org.springframework.web.context.ContextLoaderListener
       </listener-class>
  </listener>
  ...

7. Demo

URL : http://localhost:8080/SpringMVC/welcome.htm

spring mvc hello world annotation demo page
Note
If you compare this Spring MVC annotation-based hello world example with previous XML-based example, you can see that this annotation approach is more easier and flexible in wiring the controller class and URL handler mapping, because you do not need to declare the controller class explicitly or extends any particular class.

Download Source Code

References

  1. Controller Javadoc
  2. RequestMapping Javadoc
  3. Spring MVC hello world XML-based example
  4. Spring auto scanning components
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • Pingback: Blue Coaster33()

  • http://www.mkyong.com Raju

    Hi really it is very useful….

  • Raju

    Extremely thanks a lot for this kind of examples….

    Please provide spring MVC videos Mkyong…..We can practice and at same time watch the video

  • Suyog

    Hi.
    MKYONG,
    I am developing above application in NetBeans with out Maven
    so here I am getting following Error plz help me on this.
    The prefix “context” for element “context:component-scan” is not bound
    For this example spring 2.5 I am using

  • Java Lover

    I am getting the below error.

    WARNING: No mapping found for HTTP request with URI [/SpringMVC/] in DispatcherServlet with name ‘mvc-dispatcher’

    Am I missing something.

  • Java Lover

    Hi
    ,
    I am getting the below error.

    WARNING: No mapping found for HTTP request with URI [/SpringMVC/] in DispatcherServlet with name ‘mvc-dispatcher’

    Am I missing something.

  • Amol

    Thanks mkyong…

    Works like a charm for me :) :)

  • help

    I’m getting this error when i tried to go on my “welcome” page :

    Avertissement: StandardWrapperValve[dispatcher]: PWC1406: Servlet.service() for servlet dispatcher threw exception
    java.lang.NullPointerException
    ...

    Could you help me finding an issue ?

  • Satish

    This is a very good website to learn new concepts very easily with the simple examples and clear explanation. Thanks a lot. Keep up the good work.

  • Andrey Kapelchik

    Are you sure that you will have no trouble finding paths in this example? The full path to your controller will be

    main.java.com.mkyong.common.controller

    when using the Maven. And you specify only

    &lt;context:component-scan base-package=&quot;com.mkyong.common.controller&quot; /&gt;

    in your mvc-dispatcher-servlet.xml.

  • Devikumar R

    Am getting the below exception. Could you please help me out?
    15:46:49,892 INFO [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/util]] (MSC service thread 1-3) Initializing Spring root WebApplicationContext
    15:46:49,908 ERROR [org.springframework.web.context.ContextLoader] (MSC service thread 1-3) Context initialization failed: org.springframework.beans.factory.BeanDefinitionStoreException: I/O failure during classpath scanning; nested exception is java.io.IOException: No context factory for vfs:/C:/Dev/jboss-as-7.1.0.Final/bin/content/Util-0.0.1-SNAPSHOT.war/WEB-INF/classes/com/clink/controller/
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:272) [spring-context-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:242) [spring-context-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.context.annotation.ComponentScanBeanDefinitionParser.parse(ComponentScanBeanDefinitionParser.java:84) [spring-context-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:73) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1419) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1409) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:184) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:140) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:111) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125) [spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94) [spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131) [spring-context-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:522) [spring-context-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:436) [spring-context-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384) [spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283) [spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111) [spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3392) [jbossweb-7.0.10.Final.jar:]
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:3850) [jbossweb-7.0.10.Final.jar:]
    at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.0.Final.jar:7.1.0.Final]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [rt.jar:1.6.0_30]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [rt.jar:1.6.0_30]
    at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_30]
    Caused by: java.io.IOException: No context factory for vfs:/C:/Dev/jboss-as-7.1.0.Final/bin/content/Util-0.0.1-SNAPSHOT.war/WEB-INF/classes/com/clink/controller/
    at org.jboss.virtual.VFS.getVFS(VFS.java:244) [com.springsource.org.jboss.virtual-2.1.0.GA.jar:2.1.0.GA]
    at org.jboss.virtual.VFS.createNewRoot(VFS.java:261) [com.springsource.org.jboss.virtual-2.1.0.GA.jar:2.1.0.GA]
    at org.jboss.virtual.VFS.getRoot(VFS.java:277) [com.springsource.org.jboss.virtual-2.1.0.GA.jar:2.1.0.GA]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.6.0_30]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [rt.jar:1.6.0_30]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [rt.jar:1.6.0_30]
    at java.lang.reflect.Method.invoke(Method.java:597) [rt.jar:1.6.0_30]
    at org.springframework.core.io.VfsUtils.invokeVfsMethod(VfsUtils.java:152) [spring-core-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.core.io.VfsUtils.getRoot(VfsUtils.java:243) [spring-core-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.core.io.support.VfsPatternUtils.findRoot(VfsPatternUtils.java:44) [spring-core-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.core.io.support.PathMatchingResourcePatternResolver$VfsResourceMatchingDelegate.findMatchingResources(PathMatchingResourcePatternResolver.java:649) [spring-core-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.core.io.support.PathMatchingResourcePatternResolver.findPathMatchingResources(PathMatchingResourcePatternResolver.java:344) [spring-core-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.core.io.support.PathMatchingResourcePatternResolver.getResources(PathMatchingResourcePatternResolver.java:266) [spring-core-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getResources(AbstractApplicationContext.java:1245) [spring-context-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:227) [spring-context-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    … 31 more

    15:46:49,970 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/util]] (MSC service thread 1-3) Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener: org.springframework.beans.factory.BeanDefinitionStoreException: I/O failure during classpath scanning; nested exception is java.io.IOException: No context factory for vfs:/C:/Dev/jboss-as-7.1.0.Final/bin/content/Util-0.0.1-SNAPSHOT.war/WEB-INF/classes/com/clink/controller/
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:272) [spring-context-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:242) [spring-context-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.context.annotation.ComponentScanBeanDefinitionParser.parse(ComponentScanBeanDefinitionParser.java:84) [spring-context-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:73) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1419) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1409) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:184) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:140) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:111) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125) [spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94) [spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131) [spring-context-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:522) [spring-context-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:436) [spring-context-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384) [spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283) [spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111) [spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3392) [jbossweb-7.0.10.Final.jar:]
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:3850) [jbossweb-7.0.10.Final.jar:]
    at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.0.Final.jar:7.1.0.Final]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [rt.jar:1.6.0_30]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [rt.jar:1.6.0_30]
    at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_30]
    Caused by: java.io.IOException: No context factory for vfs:/C:/Dev/jboss-as-7.1.0.Final/bin/content/Util-0.0.1-SNAPSHOT.war/WEB-INF/classes/com/clink/controller/
    at org.jboss.virtual.VFS.getVFS(VFS.java:244) [com.springsource.org.jboss.virtual-2.1.0.GA.jar:2.1.0.GA]
    at org.jboss.virtual.VFS.createNewRoot(VFS.java:261) [com.springsource.org.jboss.virtual-2.1.0.GA.jar:2.1.0.GA]
    at org.jboss.virtual.VFS.getRoot(VFS.java:277) [com.springsource.org.jboss.virtual-2.1.0.GA.jar:2.1.0.GA]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.6.0_30]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [rt.jar:1.6.0_30]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [rt.jar:1.6.0_30]
    at java.lang.reflect.Method.invoke(Method.java:597) [rt.jar:1.6.0_30]
    at org.springframework.core.io.VfsUtils.invokeVfsMethod(VfsUtils.java:152) [spring-core-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.core.io.VfsUtils.getRoot(VfsUtils.java:243) [spring-core-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.core.io.support.VfsPatternUtils.findRoot(VfsPatternUtils.java:44) [spring-core-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.core.io.support.PathMatchingResourcePatternResolver$VfsResourceMatchingDelegate.findMatchingResources(PathMatchingResourcePatternResolver.java:649) [spring-core-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.core.io.support.PathMatchingResourcePatternResolver.findPathMatchingResources(PathMatchingResourcePatternResolver.java:344) [spring-core-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.core.io.support.PathMatchingResourcePatternResolver.getResources(PathMatchingResourcePatternResolver.java:266) [spring-core-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getResources(AbstractApplicationContext.java:1245) [spring-context-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:227) [spring-context-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    … 31 more

  • Dan

    You got a special trick to explain complex things in simple way.. I am not only meaning this post but all of them

    • http://www.mkyong.com mkyong

      Thanks for your kind words.

  • hazarath

    hi,
    how to get spring configurations(xml files) files, where you get those files.

  • giri

    sir,
    pls tell how to change Projectname from “SpringMVC” to “MyProjectName”

    • http://www.mkyong.com mkyong

      The project name is doesn’t matter, and you can configure in mapping path. To fix it, just change the project folder name, and update the web.xml file.

  • Ram

    All of your tutorials are very nice,
    Why do you add spring configurations/annotations unit testing examples.

  • C

    Tried doing this with the latest Spring release but it did not compile – somehow it appears that the Controller and RequestMapping/RequestMethod classes must have moved. Still trying to figure that out.

    • C

      Changed the POM’s spring entries to [3.1.0,) to get the latest, the code compiles now – but maven still throws up on the other dependencies in the tutorial.

      • C

        Fixed my maven issues, but still getting a 404 error when trying to point my browser to the local tomcat ‘welcome.htm’. Any ideas?

        • C

          Never mind, she works :-) more maven issues, but overall a nice job with the tutorial!

  • Ari

    Was anyone able to run this example successfully?
    I’m getting 404 not found for this URL:
    http://localhost:8080/SpringMVC/welcome.htm

    • http://www.mkyong.com mkyong

      Well tested, and it’s working fine, did you find any error at your console or log file?

      • Ari

        I’m using eclipse and a tomcat plugin to run the app

        how did you package the app ?
        did you run it on a standalone tomcat ?

        • http://www.mkyong.com mkyong

          I’m using Maven, just “mav war:war” or build it, Maven will pack everything for deployment.

    • Thuy

      Hi mkyong,
      I still get 404 error although I try a lot and search Google a lot. It don’t run Controller, it seem that mvc-dispatcher does not work. There is nothing error in console log. Could you please recheck your coding. or you can give me file war instead?
      Do you give me some hint for fix

  • Ethan Lew

    Simple and nice example.

    Just 1 question. Could we still make use of

    HttpServletRequest

    and

    HttpServletResponse

    in annotation-based controller method?

  • varun jain

    why i am facing this issue ?

    SEVERE: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
    java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1387)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3877)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4429)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:722)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(StandardService.java:516)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:583)
    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:288)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)

    • http://www.mkyong.com mkyong

      bro, you didn’t include Spring jars, build the downloaded project with Maven, it will download the entire project dependencies.

      • varun jain

        Thanks Bro

        can u help me to understand beans lifecycle in spring MVC ?
        When beans declared for any controller will be created ?

        what abt controller instance ?
        what abt bean instance ?

        • http://www.mkyong.com mkyong

          Normally, beans declared in Spring Ioc either XML file or auto scanning mode, will be created during your we project start up phase.

          For detail explanation, or you want to study deep inside the bean life cycle, refer to this Spring doc or this.

          • varun jain

            Thanks dude.

  • Pingback: Spring MVC form handling annotation example()

  • Pingback: Spring MVC hello world example()

  • sikinder

    Hi Yong,
    Thanks for the simplest example of Annotations.

    • Jayaprakash

      Which one do you prefer,whether to use annotation based or make use of spring-view.xml

      • http://www.mkyong.com mkyong

        I’m an old developer, more prefer XML-based, because it’s more manageable.

  • anu

    I followed the same steps as given here but I am getting resource not found. Do I need to include the below:-

    Looking for a reply.

    • http://www.mkyong.com mkyong

      What your resource is not found? Please build with Maven.

  • http://www.saggezza.com Pritam Ghosh

    Awesome Tutorial……Thanks for such easy to understand tuttorial….

  • bogdan_ro

    great great great !! thank you !

  • Student

    Executed the same code but getting error
    noHandlerFound No mapping found for HTTP request with URI

  • Harshal Patel

    This is good website for creating and understanding the Spring Annotation…i have created my first annotation based Application using the help of this website….
    So thanks a lot for providing such a nice Guidance to the developer…

    • http://www.mkyong.com mkyong

      Welcome, thanks for the kind comment, we all learn thought sharing…

  • Pingback: Spring3MVCApplication « Vbtperk's Blog()

  • ???

    thanks , it really helped me out

  • http://None Phu Phan

    One word… Awesome.
    I read through all of your posts!! well maybe 99% of it.
    Keep it up Mykong,
    Thanks.

    • http://www.mkyong.com mkyong

      Thanks for your encouraging reply.

  • rohan

    Thanks a lot for this kind of example… very clear vision..

    Thanks a ton…. this website has been most favorable for me to learn Struts and spring framework..

    Please keep updating us by such good example.

  • Pingback: Spring MVC Tutorials()