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.

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

     <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.do</url-pattern>
     </servlet-mapping>
     	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
     <context-param>
	    <param-name>contextConfigLocation</param-name>
	    <param-value>classpath:applicationContext.xml</param-value>
     </context-param>

 

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

@Controller
public class ContactsControllers
{
	@RequestMapping("/searchContacts")
	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/searchContacts.do request will be forwarded to above controller and controller will forward the given request to “WEB-INF/jsp/showContacts.jsp

5 Comments

Leave a Reply

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.