In this java tutorial, we will address Spring MVC's annotation based configuration. We can easily create our controller with a simple class level annotation. Then we'll have to create a method that will handle our request/response and forward that request/response to a particular view. With spring's view resolver. In this example, we will add an Object (a message String) to the request and display it.

Maven configuration

You need the following jar files. So either add this pom.xml file to your application or download and add the jar files to your class-path.


In spring MVC any java POJO can be a controller. The only thing we have to do is annotate our class with @Controller, and register component scanning in our application context and spring will automatically make it a MVC controller. Next we annotate our class with @RequestMapping("hello") witch means that every method in our class automatically maps to /hello. If we annotate another @RequestMapping("hello") at method level then the controller can be accessed at /hello/hello. you get the picture.. In the request mapping annotation we can also specify a RequestMethod like: GET, POST, DELETE, PUT.

We return a ModelAndView instance witch we can add Objects to and specify a view name for our view resolver to handle.

package org.camelcode.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

public class HelloController {

	@RequestMapping(method = RequestMethod.GET)
	public String printWelcome(ModelMap model) {
		model.addAttribute("message", "Hello, World!");
		return "index";