Spring MVC Hello World Example-Spring 3 Mvc Tutorial

This Spring 3 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 3 Mvc web project Steps

1)Create a web project in eclipse
Select File->New->Dynamic Web Project. We have named it SpringMvcHelloWorld. Select Tomcat as the run time environment.

mvc1

 

2)Adding the jar files and create the project structure
We will add all the downloaded jar files into the lib folder of the WEB-INF directory. Below is the  of the folder structure of the web project in eclipse.

Spring-Mvc-Folder-Structure-Eclipse

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

4)Spring Configuration File
We will create a xml file named spring-mvc-servlet.xml
Based on the <servlet-name> entered in the web.xml for the DispatcherServlet we will create a xml file whose name will be <servlet-name>+”-servlet”.xml.  So in our case it will be spring-mvc-servlet.xml. By default the DispatcherServlet looks for a xml file with “-servlet” appended to the name to load spring configurations. But it can also be configured explicitly from which xml file DispatcherServlet should load Spring Configurations.

Below entry will be required only if the name of the xml is not named according to <servlet-name>+”-servlet”.xml.

5)A HelloWorldController class
We will create a controller class named HelloWorldController in the package online.java.tutor

@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

 

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
We should create hello.jsp and hi.jsp in the pages folder in WEB-INF

hello.jsp

 hi.jsp

 

 

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.