Spring 3 MVC hello world example
Before you start this Spring 3 MVC tutorial, please refer to this new features in Spring 3 documentation, so that you have a brief idea of what’s new in Spring 3.
In this tutorial, we show you how to develop a Spring 3 MVC hello world example.
Technologies used :
- Spring 3.0.5.RELEASE
- JDK 1.6
- Maven 3
- Eclipse 3.6
1. Project Dependency
In Spring 3 @MVC, declares following dependencies in your Maven pom.xml file.
<properties> <spring.version>3.0.5.RELEASE</spring.version> </properties> <dependencies> <!-- Spring 3 dependencies --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> </dependencies> </project>
2. Controller & Mapping
In Spring 3, annotation is widely adapted in everywhere. The @RequestMapping is available since 2.5, but now enhanced to support REST style URLs in Spring MVC.
package com.mkyong.common.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/welcome") public class HelloController { @RequestMapping(method = RequestMethod.GET) public String printWelcome(ModelMap model) { model.addAttribute("message", "Spring 3 MVC Hello World"); return "hello"; } }
3. JSP Views
A JSP page to display the value.
File : hello.jsp
<html> <body> <h1>Message : ${message}</h1> </body> </html>
4. Spring Configuration
In Spring 3, you still need to enable “auto component scanning” (for controller) and declares “view resolver” manually.
File : mvc-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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="com.mkyong.common.controller" /> <bean 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>
5. Integrate Web application with Spring
Integration is no different if compare with old Spring 2.5.6, just declares Spring “ContextLoaderListener” and “DispatcherServlet“.
File : web.xml
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Spring MVC Application</display-name> <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>/</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> </web-app>
6. Demo
URL : http://localhost:8080/SpringMVC/welcome

Download Source Code
References
- Spring 3 hello world example
- What new in Spring 3
- Spring 3 MVC and JSR303 @Valid example
- Spring 3 MVC and RSS feed example
- Spring 3 MVC and XML example
- Spring 3 MVC and JSON example
- Spring 3 REST hello world example
- Spring 2.5.6 MVC hello world example
- Spring 2.5.6 MVC hello world annotation example






Good Starting for WEB MVC..
I have a question . we have
is able to find the @Controllers and able to handle my request then
what is the use of ?
Raj
in this example one problem is there. for running this one more you need to add and its
name is org.springframework.context-3.0.5.RELEASE.jar. then you can compile code.
otherwise you will get compilation error.
how to hide parameter in url example ..
http://localhost:9090/LifecareEMR/ChiefComplaintsEdit.html?id=8
i want to hide id=8 i am using spring & hibernate MVC form..
plz give me help
Dumb question but… how can I display image or include css file to the jsp file?
For example if my hello.jsp looks like:
Message : ${message}And image.jpg file is in webapp folder.
Where exactly should be image.jpg file in project structure, and is that url (“/SpringMVC/image.jpg”) correct? Because I try to add some jpg and css files in several ways and for now without effect.
My hello.jsp file:
Message : ${message}
Hi,
I have downloaded the above example and imported to my eclipse workspace as a maven project and i didn’t change any thing.I just run that project as RunAs -> Maven Install and after that i copied the war file to the webapps of tomcat and i just started the tomcat.I hit the following URL
http://localhost:8080/SpringMVC/welcome
I am getting the following Exception….
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/apache/el/ExpressionFactoryImpl
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:268)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.lang.NoClassDefFoundError: org/apache/el/ExpressionFactoryImpl
org.apache.jasper.JspCompilationContext.getDerivedPackageName(JspCompilationContext.java:454)
org.apache.jasper.JspCompilationContext.getServletPackageName(JspCompilationContext.java:443)
org.apache.jasper.JspCompilationContext.createOutputDir(JspCompilationContext.java:668)
org.apache.jasper.JspCompilationContext.getOutputDir(JspCompilationContext.java:197)
org.apache.jasper.JspCompilationContext.getClassFileName(JspCompilationContext.java:514)
org.apache.jasper.compiler.Compiler.isOutDated(Compiler.java:453)
org.apache.jasper.compiler.Compiler.isOutDated(Compiler.java:399)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:585)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.lang.ClassNotFoundException: org.apache.el.ExpressionFactoryImpl
java.net.URLClassLoader$1.run(URLClassLoader.java:366)
java.net.URLClassLoader$1.run(URLClassLoader.java:355)
java.security.AccessController.doPrivileged(Native Method)
java.net.URLClassLoader.findClass(URLClassLoader.java:354)
java.lang.ClassLoader.loadClass(ClassLoader.java:423)
java.lang.ClassLoader.loadClass(ClassLoader.java:356)
org.apache.jasper.JspCompilationContext.getDerivedPackageName(JspCompilationContext.java:454)
org.apache.jasper.JspCompilationContext.getServletPackageName(JspCompilationContext.java:443)
org.apache.jasper.JspCompilationContext.createOutputDir(JspCompilationContext.java:668)
org.apache.jasper.JspCompilationContext.getOutputDir(JspCompilationContext.java:197)
org.apache.jasper.JspCompilationContext.getClassFileName(JspCompilationContext.java:514)
org.apache.jasper.compiler.Compiler.isOutDated(Compiler.java:453)
org.apache.jasper.compiler.Compiler.isOutDated(Compiler.java:399)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:585)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
Why i am getting this exception i don’t know.I didn’t modify anything in the code…Can you please tell me the reason…..
Hi Sir,
Thank for this tutorial
I’m working with Tomcat 7.0 , and i deployed the application on Tomcat but when starting the server i’m getting this error ;
…
…
Infos: Server startup in 2846 ms
janv. 26, 2012 12:44:17 PM org.apache.catalina.core.StandardWrapperValve invoke
Grave: Servlet.service() for servlet [jsp] in context with path [] threw exception [java.lang.VerifyError: (class: org/apache/jasper/runtime/JspApplicationContextImpl, method: createELResolver signature: ()Ljavax/el/ELResolver;) Incompatible argument to function] with root cause
java.lang.VerifyError: (class: org/apache/jasper/runtime/JspApplicationContextImpl, method: createELResolver signature: ()Ljavax/el/ELResolver;) Incompatible argument to function
at org.apache.jasper.runtime.JspFactoryImpl.getJspApplicationContext(JspFactoryImpl.java:209)
at org.apache.jsp.index_jsp._jspInit(index_jsp.java:31)
at org.apache.jasper.runtime.HttpJspBase.init(HttpJspBase.java:49)
at org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:181)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:370)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:185)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:269)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Thank you in advance
I got an error
21 Dec, 2011 12:14:46 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound WARNING: No mapping found for HTTP request with URI [/BOL/home] in DispatcherServlet with name ‘mvc-dispatcher’
I have gone wrone somewhere. I just modified your codes at couple of places. I tried your code also directly and got the same error. I posted it here but didt get any reply http://stackoverflow.com/questions/8580428/spring-3-no-mapping-found-for-http-request-beginner
The HelloController.java need update, as RequestMapping ask for url declared with property style, value=?, for example
@Akhil,
I was getting the same error… this is how I fixed it – hope it helps.
To get rid of the “No mapping found…” error make sure you have a “classes” folder under “WEB-INF” folder and also make sure that this classes folder contains the compiled “.class” file of your java class “HelloController.java”.
If you don’t see any compiled .class file in that folder then you definitely have any error in your project, fix that error and eclipse will compile the class for you.
Also right-click project then select “properties” then select “Java build path” then click on “Source” tab and set the WEB-INF/classes as your Default output folder.
Thanks a lot Rizwan
You save my night :-)
After having thousands of problems with maven (that i finally solved by myself) i had the same problem
Your solution is the wright one
By default, My compiled classes were put in target/classes
Hi, Thanks for the very useful example.I’m facing a problem with it,I have done every thing as you mentioned but only instead of maven I used eclipse with pre-downloaded JAR files.the problem is the the controller is not replacing the message variable with the actual value and showin the message as ${message}!
Can you help me to overcome this issue please?
Hi borzou,
Please put following line on top of your jsp page:
It works =)
i also was try to debug the damn thing..
the example is using a web-app_2_4 schema so it get the false by default.
much appreciated
You said that:
In Spring 3 @MVC, declares following dependencies in your Maven pom.xml file
So, do we need to write anything in POM.xml file or we should keep it blank and everything what you have mentioned above will be added automatically.
And if we need to write then what part we have to add and what part will be added automatically ?
Download the example, and see what inside the pom.xml. At first, learn Maven.
[...] it – Spring-Security-3-HelloWorld-Example.zip (8 KB)ReferencesSpring Security Features Spring 3 MVC hello world example [...]
[...] MVC Spring MVC Quick StartLet go through a quick start to understand how Spring MVC framework works.Spring 3 MVC hello world example what you need to develop Spring 3 MVC web application.Spring 3 REST hello world example Develop [...]
[...] July 29, 2010 , Updated: August 2, 2011 , Author: mkyongprint Spring 3 You may interest at this Spring 3 MVC hello world example.In Spring MVC web application, it consist of 3 standard MVC (Model, Views, Controllers) components [...]
[...] 2011 , Author: mkyongprint 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. P.S [...]