In this example show how to write a simple web based Hello World application using Spring MVC framework. To start with it, let us have working with STS IDE in place and follow the following steps to develop a Dynamic Web Application using Spring Web MVC Framework:
Step 1: Create a Dynamic Web Project with a name SpringMVCHelloWeb and create a package com.dineshonjava.controller under the src folder in the created project.
Step 2: Add below mentioned Spring 3.0 libraries and other libraries into the folder WebRoot/WEB-INF/lib.
/SpringMVCHelloWorld/WebRoot/WEB-INF/lib/commons-fileupload-1.1.1.jar
/SpringMVCHelloWorld/WebRoot/WEB-INF/lib/commons-io-1.2.jar
/SpringMVCHelloWorld/WebRoot/WEB-INF/lib/commons-logging-1.1.1.jar
/SpringMVCHelloWorld/WebRoot/WEB-INF/lib/hibernate-validator-4.0.2.GA.jar
/SpringMVCHelloWorld/WebRoot/WEB-INF/lib/jstl-1.2.jar
/SpringMVCHelloWorld/WebRoot/WEB-INF/lib/log4j-1.2.14.jar
/SpringMVCHelloWorld/WebRoot/WEB-INF/lib/servlet-2.3.jar
/SpringMVCHelloWorld/WebRoot/WEB-INF/lib/slf4j-api-1.5.6.jar
/SpringMVCHelloWorld/WebRoot/WEB-INF/lib/slf4j-log4j12-1.5.6.jar
/SpringMVCHelloWorld/WebRoot/WEB-INF/lib/spring-asm-3.0.3.RELEASE.jar
/SpringMVCHelloWorld/WebRoot/WEB-INF/lib/spring-beans-3.0.3.RELEASE.jar
/SpringMVCHelloWorld/WebRoot/WEB-INF/lib/spring-context-3.0.3.RELEASE.jar
/SpringMVCHelloWorld/WebRoot/WEB-INF/lib/spring-core-3.0.3.RELEASE.jar
/SpringMVCHelloWorld/WebRoot/WEB-INF/lib/spring-expression-3.0.3.RELEASE.jar
/SpringMVCHelloWorld/WebRoot/WEB-INF/lib/spring-web-3.0.3.RELEASE.jar
/SpringMVCHelloWorld/WebRoot/WEB-INF/lib/spring-webmvc-3.0.3.RELEASE.jar
/SpringMVCHelloWorld/WebRoot/WEB-INF/lib/validation-api-1.0.0.GA.jar
Step 3: Create a Java class HelloWorldController under the com.dineshonjava.controller package.
Step 4: Create Spring configuration files Web.xml and sdnext-servlet.xml under the WebRoot/WEB-INF/ folder.
Step 5: Create a sub-folder with a name views under the WebRoot/WEB-INF folder. Create a view file hello.jsp under this sub-folder.
Step 6: The final step is to create the content of all the source and configuration files name sdnext-servlet.xml under the sub-folder WebRoot/WEB-INF/config and export the application as explained below.
Software versions used to run the sample code:
- Spring 3.0
- Java 1.6
- Tomcat 7
- JSTL 1.2
- STS Java EE IDE
Web Application Structure:
Here is the content of HelloWorldController.java file
package com.dineshonjava.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; /** * @author Dinesh Rajput * */ @Controller @RequestMapping("/hello") public class HelloWorldController { @RequestMapping(method = RequestMethod.GET) public String sayHelloWorld(ModelMap model) { model.addAttribute("message", "Spring 3.0 MVC Framework Hello World Example!"); model.addAttribute("auther", "DineshOnJava"); return "hello"; } }
Following is the content of Spring Web configuration file web.xml
<web-app id="WebApp_ID" version="3.0" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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"> <display-name>SpringMVCHelloWorld</display-name> <welcome-file-list> <welcome-file>/</welcome-file> </welcome-file-list> <servlet> <servlet-name>sdnext</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name><param-value>/WEB-INF/config/sdnext-servlet.xml</param-value></init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>sdnext</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
Following is the content of another Spring Web configuration file sdnext-servlet.xml
<beans xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- Enable annotation driven controllers, validation etc... --> <mvc:annotation-driven></mvc:annotation-driven> <context:component-scan base-package="com.dineshonjava.controller"> </context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
Following is the content of Spring view file hello.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <html> <head> <title>Spring 3.0 MVC Hello World Example</title> </head> <body> <h2> ${message}</h2> <h2> ${auther}</h2> </body> </html>
Once you are done with creating source and configuration files, export your application. Right click on your application and use Export -> WAR File option and save your SpringMVCHelloWorld.war file in Tomcat’s webapps folder.
Now start your Tomcat server and make sure you are able to access other web pages from webapps folder using a standard browser. Now try to access the URL http://localhost:8080/sdnext/hello and if everything is fine with your Spring Web Application, you should see the following result:
You should note that in the given URL, sdnext is the application name and hello is the virtual subfolder which we have mentioned in our controller using @RequestMapping(“/hello”). You can use direct root while mapping your URL using @RequestMapping(“/”), in this case you can access the same page using short URL http://localhost:8080/sdnext / but it is advised to have different functionalities under different folders.
Download Source Code
SpringMVCHelloWorld.zipĀ
- Spring MVC Web Tutorial
- Spring MVC Interview Questions
- MVC Design Pattern
- Spring MVC DispatcherServlet
- Spring MVC WebApplicationContext and Root Application Context
- Spring MVC @Controller Annotation
- Spring MVC @RequestMapping Annotation
- Spring MVC @RequestParam Annotation
- Spring MVC ContextLoaderListener
- Spring MVC @RequestParam and @PathVariable annotations
- Spring MVC Hello World Example
- Spring MVC Exception Handling Example
- Spring MVC with Hibernate CRUD Example
- Spring MVC Tiles Plugin with Example
- Spring MVC Interceptor with example
- Spring MVC with MongoDB CRUD Example
- Spring MVC Internationalization & Localization with Example
Hi Dinesh,
I am struggling to create first spring mvc app.
Thanks for this post with jar files.
Hi Dharmendra,
We hope you are success fully deployed your application.
Keep learning from same its good.
Thanks
Dinesh
I am getting this error can u help me out
HTTP Status 404 – Servlet sdnext is not available
——————————————————————————–
type Status report
message Servlet sdnext is not available
description The requested resource (Servlet sdnext is not available) is not available.
Hi Friend,
Please update context root(sdnext) of your application. Default name of context root is same as project name after that redeploy application. Then you will get successfully running.
Thanks,
Dinesh
Hi Dinesh,I could not understand, can u explain regarding this more briefly..
hi,
plese help me out,
i am getting an error when i start the tomcat server:
SEVERE: Error starting static Resources
java.lang.IllegalArgumentException: Document base D:SoftwaresProgram softwaresapache-tomcat-7.0.22webappsSpringMVCHelloProgram does not exist or is not a readable directory
Hi,
Friend I thinks your tomcat does not read application. For resolving this firstly you delete all application on tomcat web application deployment directory (webapps)
D:SoftwaresProgram softwaresapache-tomcat-7.0.22webapps
After that deploy this application again. Then we hope you will get successfully start apps.
Please like this site on facebook also for regularly updates.
Thanks,
Dinesh
Hi friends,
please check web.xml file, are you defined servlet with name sdnext.
<servlet>
<servlet-name>sdnext</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name><param-value>/WEB-INF/config/sdnext-servlet.xml</param-value></init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sdnext</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
Thanks,
Dinesh
Thank u Dinesh..U r so Kind in giving the response,
Thanks very much Dinesh
Hai I am getting
server: Servlet /SpringMVCHelloworld threw load() exception
java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:525)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:507)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:126)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1099)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1043)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4957)
at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5284)
at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5279)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
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)
hello sir i have same error HTTP Status 404 . i follow all steps as you told but it give this same error.