Spring Hello World

In this tutorial we will learn to develop Hello world example based on Spring MVC.

Technology Used

  • Spring 3.1.0(Requires Java 1.5+)
  • Java 1.6

Tools Used

  • Eclipse Helios

 

Web Project Creation

In eclipse we will create Dynamic web project. For that go to File » New » Other » Dynamic Web Project

Eclipse will create blank web project. Now we need to configure in a way that all the request forward via spring framework dispatcher. Make below entry in web.xml.

 

Now all the request with .do extension will pass through Spring dispatcher and dispatcher will decide the controller which will handle the given request.


 

Controller Configuration

Now we need to configure the controller which will handle different request URLs. Spring 3 support annotation and we can use @Controller & @RequestMapping to map the request to exact controller.

To enable annotation we need to make below configuration in applicationContext.xml.

 

<mvc:annotation-driven /> is used to activate the annotation driven architecture.
<context:component-scan base-package/> is used to provide the package where spring should look for the controllers.


 

Controller Class Configuration


@Controller is used to mention that this controller class.
@RequestMapping is used to define the URL patter which given method will handle.


 

View Resolver

Till now we are done with Controller and now we need to configure the view part. We need to write below entry in dispatcher-servlet.xml

 

Now you are done with configuration. If you hit the URL localhost:8080/contextRoot/searchContacts.do request will be forwarded to above controller and controller will forward the given request to “WEB-INF/jsp/showContacts.jsp

4 Comments Spring Hello World

  1. achilles ram

    can i have an example on @Configuration with complete detail explanation.. please help me… my mail id [email protected]

    Reply
  2. Anand Jain

    A very good approach to teach… thanks for sharing your experience …It helped alot…

    Thanks again

    Reply

Leave A Comment

Your email address will not be published. Required fields are marked *