Spring MVC Hello World Example

In this example, we will create our first Spring MVC Hello World application.


1. Technologies used

  • Spring 3.2.8.RELEASE
  • Maven 3.0.5
  • Eclipse 4.2
  • JDK 1.6
  • Tomcat 6.0


2. Project structure

We create a Maven ‘s web application.



3. Project dependencies

We add Spring MVC dependencies in Maven’ s pom.xml file as shown below :


4. Controller

To implement our HelloController, we use the @Controller and @RequestMapping annotations.

We added the sayHello(..) method that accepts a Model and returns the welcome String as logical view name.

We put the Hello World message in the Model.


5. JSP view

It will represent the Model to the user by displaying Hello World message.

File : welcome.jsp


6. Spring MVC configuration

It contains all of the application beans.

File : mvc-dispatcher-servlet.xml

We enabled component scanning to autodetect our annotated controller.

We added a view resolver InternalResourceViewResolver to resolve the logical view names returned by the controller methods.

In our example, the logical view welcome will be resolved into the /WEB-INF/pages/welcome.jsp view.


7. Web app configuration

We must declare the DispatcherServlet that is responsible for dispatching requests to controllers.

In our example, we tell that all requests ending with *.htm should be handled by the DispatcherServlet instance.

File : web.xml

We passed the name of Spring config file to the context instance.


8. Test It

URL : http://localhost:8080/Spring-MVC-Hello-World/hello.htm




Download source code