Spring 3
This tutorial is using Spring 2.5.6, you may interest at this Spring 3 MVC hello world example.

In this tutorial, we show you how to create a Spring @MVC annotation-based hello world example.

Technologies used :

  1. Spring 2.5.6
  2. JDK 1.6
  3. Maven 3
  4. Eclipse 3.6

P.S This annotation-based example is converted from last Spring MVC hello world XML-based example.

1. Spring Dependency

Spring @MVC is bundled in the same spring-webmvc.jar. Declares following dependencies in your Maven pom.xml file.

	<!-- Spring framework --> 
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring</artifactId>
		<version>2.5.6</version>
	</dependency>
 
       <!-- Spring MVC framework --> 
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
		<version>2.5.6</version>
	</dependency>

2. Controller & Handler Mapping

Now, you can use @Controller and @RequestMapping to replace the XML configuration.

  1. Controller – The controller class is no longer need to extends base controller like AbstractController or SimpleFormController, just simply annotate the class with a @Controller annotation.
  2. Handler Mapping – No more declaration for the handler mapping like BeanNameUrlHandlerMapping, ControllerClassNameHandlerMapping or SimpleUrlHandlerMapping, all are replaced with a standard @RequestMapping annotation.

File : HelloWorldController.java

package com.mkyong.common.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
@RequestMapping("/welcome")
public class HelloWorldController{
 
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView helloWorld(){
 
		ModelAndView model = new ModelAndView("HelloWorldPage");
		model.addObject("msg", "hello world");
 
		return model;
	}
}

If the @RequestMapping is applied at class level (can apply at method level with multi-actions controller), it required to put a RequestMethod to indicate which method to handle the mapping request.

In this case, if an URI pattern “/welcome” is requested, it will map to this HelloWorldController, and handle the request with helloWorld() method.

3. View Resolver

Sadly, you are still required to configure the view resolver.

<beans ...>
 
     <bean id="viewResolver"
      	   class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
           <property name="prefix">
              <value>/WEB-INF/pages/</value>
           </property>
           <property name="suffix">
              <value>.jsp</value>
           </property>
     </bean>
 
</beans>

4. View

A simple JSP for demonstration.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
	<h1>Spring MVC Hello World Annotation Example</h1>
 
	<h2>${msg}</h2>
 
</body>
</html>

5. Components Auto Scanning

To make Spring @MVC annotation work, you have to enable Spring’s auto component scanning feature through the <context:component-scan> element.

<beans ...
   xmlns:context="http://www.springframework.org/schema/context
   ...
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 
	<context:component-scan base-package="com.mkyong.common.controller" />
 
</beans>
Note
You may interest at this Spring auto scanning components article.

Spring Configuration
See a complete Spring configuration file.

File : mvc-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 
	<context:component-scan base-package="com.mkyong.common.controller" />
 
	<bean id="viewResolver"
      	   class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
           <property name="prefix">
              <value>/WEB-INF/pages/</value>
           </property>
           <property name="suffix">
              <value>.jsp</value>
           </property>
        </bean>
 
</beans>

6. web.xml

No exception, you are still required to configure the web.xml to enable the Spring MVC features.

File : web.xml

 ...
 <servlet>
  	<servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
  </servlet>
 
  <servlet-mapping>
 	<servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
  </servlet-mapping>
 
  <context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
  </context-param>
 
  <listener>
       <listener-class>
          org.springframework.web.context.ContextLoaderListener
       </listener-class>
  </listener>
  ...

7. Demo

URL : http://localhost:8080/SpringMVC/welcome.htm

spring mvc hello world annotation demo page
Note
If you compare this Spring MVC annotation-based hello world example with previous XML-based example, you can see that this annotation approach is more easier and flexible in wiring the controller class and URL handler mapping, because you do not need to declare the controller class explicitly or extends any particular class.

Download Source Code

References

  1. Controller Javadoc
  2. RequestMapping Javadoc
  3. Spring MVC hello world XML-based example
  4. Spring auto scanning components
Tags :
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider making a donation to the charity, thanks.

Related Posts

Popular Posts