spring-mvc

Spring-MVC with annotations

Introduction#

In this topic you’ll read about annotations mainly related to Spring MVC. Some of the related annotations are as follows: @Controller, @RequestMapping, @RequestParam, @RequestBody, @ResponseBody, @RestController, @ModelAttribute, @ControllerAdvice, @ExceptionHandler, @ResponseStatus.

Of course there’re more annotations which are extremly important as well but not belong directly to Spring MVC. Such as: @Required, @Autowired, @Resource, and many more.

Parameters#

Annotation Explanation
@Controller With @Controller annotation you mark a Java Class as a Class that holds HTTP handlers, in other words, HTTP access points to your application.
@RequestMapping The @RequestMapping annotation is the one that you’ll use to mark HTTP handlers (HTTP access points to your application) within your @Controller Class
@RequestParam Use the @RequestParam annotation to bind request parameters to a method parameter in your controller.

dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
    xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="https://www.springframework.org/schema/mvc"
    xmlns:context="https://www.springframework.org/schema/context"
    xsi:schemaLocation="https://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-4.3.xsd">
    
    
    <mvc:annotation-driven/>
    <context:component-scan base-package="your.base.package.to.scan" />
</beans>

With these two lines of configuration, you’ll enable the usage of MVC annotations.

@Controller & @RequestMapping

@Controller
@RequestMapping("/appointments")
public class AppointmentsController {

//your handlers here, for example:

@RequestMapping(path = "/new", method = RequestMethod.GET)
public AppointmentForm getNewForm() {
    return new AppointmentForm();
}

@RequestMapping(method = RequestMethod.POST)
public String add(@Valid AppointmentForm appointment, BindingResult result) {
    if (result.hasErrors()) {
        return "appointments/new";
    }
    appointmentBook.addAppointment(appointment);
    return "redirect:/appointments";
}

}

With @Controller annotation you’ll mark a Java Class as a Class that holds several HTTP handlers, in other words, HTTP access points to your application.

The @RequestMapping annotation is the one that you’ll use to mark HTTP handlers (HTTP access points to your application) within your @Controller Class

@RequestParam

@Controller
public class EditPetForm {

    @RequestMapping("/pets")
    public String setupForm(@RequestParam("petId") int petId, ModelMap model) {
        Pet pet = this.clinic.loadPet(petId);
        model.addAttribute("pet", pet);
        return "petForm";
    }
}

Important to mention, but pretty obvious, is that @RequestParam is intended to work when using HTTP GET method only because only with GET you can send a query string with parameters, and @RequestParam you can bind parameters in the query string to your controller handler parameters.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow