Spring Mvc Hello World Example
This Spring Mvc tutorial is for creating a simple Hello World program step by step.
The requirements for this tutorial are as follows:
1)Download JDK(Java Development Kit) here. Select the version based on your OS.
2)Download Eclipse from http://www.eclipse.org/downloads You can select Eclipse IDE for Java EE Developers 32/64 bits depending on your OS.
3)Download Spring jar files here. For this tutorial we will use 3.2.0.Release, also download commons-logging-1.1.1.jar file here.
Hello World Spring Mvc web project Steps
1) Create a Dynamic web project in eclipse
2) Adding the jar files and create the project structure
3) Configure the Front Controller
Adding the <servlet-mapping> of DispatcherServlet in web.xml
We add the <servlet> block and the <servlet-mapping> block in the web.xml for the DispatcherServlet. By putting / in the <servlet-mapping> all the requests will go through the DispatcherServlet which acts like a FrontController in spring framework.
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SpringMvcHelloWorld</display-name> <servlet> <servlet-name>spring-mvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> |
4) Spring Configuration File
Below entry will be required only if the name of the xml is not named according to <servlet-name>+”-servlet”.xml.
1 2 3 4 5 6 7 8 9 10 | <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/SpringMVCBeans.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> |
5) A HelloWorldController class
We will create a controller class named HelloWorldController in the package online.java.tutor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package online.java.tutor; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloWorldController { @RequestMapping({"/","/hello"}) public String hello() { return "hello"; } @RequestMapping("/hi") public String hi() { return "hi"; } |
@Controller annotation above the HelloWorldController class indicates that this is a controller class.This annotation is a specialization of the annotation @Component , which means <context: component-scan> will register @Controller-annotated classes as beans, as if they were annotated with @Component.
We need to configure a <context:component-scan> in spring-mvc-servlet.xml so that the HelloWorldController class (and all of the other controllers) will be automatically discovered and registered as beans.
<context:component-scan base-package=”online.java.tutor” />
The @RequestMapping annotation above the functions identify them as the request handling methods.
@RequestMapping({“/”,”/hello”}) above the hello() indicates that it will get executed with the default path contains “hello”.
@RequestMapping(“/hi”) indicates hi() will be executed when the path contains “hi”
The hello() function returns a String “hello” and hi() function returns “hi”. These returned String values are the logical name of the view that should render the results.
The DispatcherServlet will use these returned names to look up the actual view implementation by consulting a view resolver.
6) spring-mvc-servlet.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 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="online.java.tutor" /> <mvc:annotation-driven /> <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> |
The InternalResourceView used above will dispatches the request to the JSP for rendering by prefixing /Web-INF/pages and appending .jsp to the logical name returned by the controller functions. So when the hello function returns “hello” the page that will render is /WEB-INF/pages/hello.jsp and similarly the hi().
7) Create view of the Mvc
hello.jsp
1 2 3 4 5 6 7 | <html> <head> /head> <body> <h1>Hello</h1> </body> </html> |
hi.jsp
1 2 3 4 5 6 7 | <html> <head> /head> <body> <h1>Hi</h1> </body> </html> |
8) Output
After deployment of the project to tomcat. When we do
http://localhost:8080/SpringMvcHelloWorld/hi on the browser we get
Hi
http://localhost:8080/SpringMvcHelloWorld/ or http://localhost:8080/SpringMvcHelloWorld/hello we get
Hello
You can download Source file of the Spring Mvc Hello World Example
SpringMvcHelloWorld
For private online java training please add me in skype.
Java Programming Training For Beginners says
Really a nice tutorial. Great post to see here. It would be really helpful for us to learn something from your blog post. Thanks for sharing it.
Khin Lay says
Thank you very much. It is very useful for me.
Mose says
First of all I would like to say fantastic blog!
I had a quick question in which I’d like to ask if you don’t mind.
I was curious to find out how you center yourself and clear your head before
writing. I have had a difficult time clearing my mind in getting
my ideas out there. I truly do take pleasure in writing however it just seems like the first 10
to 15 minutes tend to be wasted just trying to figure
out how to begin. Any suggestions or hints? Kudos!