Scroll to top

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.

	<context:component-scan base-package="packagename" />

	<mvc:annotation-driven />


<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

public class ContactsControllers
	public ModelAndView searchContacts(@RequestParam(required= false, defaultValue="") String name)
		ModelAndView mav = new ModelAndView("showContacts");
		return mav;

@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

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    	  p:prefix="/jsp/" p:suffix=".jsp" />


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

Author avatar


  1. antonio persichini

    very good tutorial

  2. Deiva

    Excellent tutorial. Keep the good work going.

  3. achilles ram

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

  4. Anand Jain

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

    Thanks again

  5. vikas

    please Explain the difference between dispatcher servlet and controller??

Post a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.