View RSS Feed

Spring Framework

How to Handle Input with Spring MVC

Rate this Entry
by , 11-29-2011 at 02:57 AM (2351 Views)
Spring MVC is structured to handle state management, workflow and validation. It is an application of the Model-View-Controller pattern. As with all things Spring, it follows the principles of loose coupling and flexibility. When a request is made, it reaches the DispatcherServlet whose role is to forward the request to the relevant Spring MVC controller, it is the controller that processes the request. In this article we will look in depth in what a Spring Controller does when it processes input.

Spring Controllers perform some logic against the information that are passed in as URL parameters or as form data. Below is an example with a Spring controller called CarController. This controller will handle several kinds of requests, many of which take input of some kind.

Java Code:
import javax.inject.Inject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; 
import com.acme.springwebapp.service.CarService;

@Controller
@RequestMapping("/cars")
public class CarController {
	private CarService carService;

	@Inject
	public CarController(CarService carService) {
		this.carService = carService;
	}

	@RequestMapping(value = "/{id}", 
			method = RequestMethod.GET)
	public String getCar(@PathVariable("id") long id, Model model) {
		model.addAttribute(carService.getCar(id));
		return "cars/view";
	}
	
	@RequestMapping(value="/cars", 
			method=RequestMethod.GET) 
	public String listCars(@RequestParam("num") long num, Model model) { 
		model.addAttribute(carService.getCars((int)num));
		return "cars/list"; 
	}
}

The CarController will handle input is in how it supports displaying a list of a given number of Cars. Note the use of @Controller and @Request- Mapping at the class level. @Controller is the indicator for <context:component-scan> that this class should be automatically discovered and registered as a bean in the Spring application context.
The @RequestMapping at the class level defines the root URL path that this controller will handle. In this controller there are several handler methods, each handling different types of requests. Here the @RequestMapping is saying that all of those requests will have paths that start with /Cars.

The CarController has two methods: listCars() and getCar(). Both handler methods are annotated with @RequestMapping.The @RequestMapping in these methods narrow the mapping defined by the class-level @RequestMapping. In this case, CarController is mapped to /Cars at the class level and to /Cars/list or to /Cars/view at the method level.

Taken together, that means that listCars() handles requests for /Cars/Cars. Moreover, the method attribute is set to GET indicating that this method will only handle HTTP GET requests for /Cars/Cars.

The listCars() method takes a Long num and a Model object as parameters.
The num parameter is annotated with @RequestParam("num") to indicate that it should be given the number of cars query parameter in the request. The getCar() method takes a String id and a Model object as its parameters. The id parameter is annotated with @RequestParam(“id”) to indicate the id for the car for which you are searching.

The second parameter in both methods, the Model object is passed in as a model element. In this case, the Controller is using a Map<String, Object> underneath. The Model provides a few convenient methods for populating the model, such as addAttribute(). The addAttribute() method does pretty much the same thing as Map’s put() method, except that it figures out the key portion of the map on its own.

When adding a Car object to the model, addAttribute() gives it the name car, it arrives at this name by applying JavaBeans property naming rules to the object’s class name. When adding a List of Cars, it tacks List to the end the member type of the List, naming the attribute carList. The CarController is now almost ready for the two methods. The only thing left to do is to create the views that will display that list of Cars or that will show the detail of the selected car.

Submit "How to Handle Input with Spring MVC" to Facebook Submit "How to Handle Input with Spring MVC" to Digg Submit "How to Handle Input with Spring MVC" to del.icio.us Submit "How to Handle Input with Spring MVC" to StumbleUpon Submit "How to Handle Input with Spring MVC" to Google

Comments