Implementing Controllers:-
Controllers provide access to the application behavior that you
typically define through a service interface. Controllers interpret user input
and transform it into a model that is represented to the user by the view.
Spring implements a controller in a very abstract way, which enables you to
create a wide variety of controllers.
Spring 2.5 introduced an annotation-based programming model for MVC
controllers that uses annotations such as @RequestMapping, @RequestParam, @ModelAttribute,
and so on. This annotation support is available for both Servlet MVC and
Portlet MVC. Controllers implemented in this style do not have to extend
specific base classes or implement specific interfaces.
@Controller
public class HelloController {
@RequestMapping("/hello")
public String helloWorld(Model model) {
model.addAttribute("message", "Hello Spring MVC");
return "hello";
}}
The @Controller and @RequestMapping annotations allow flexible method names and signatures. In this particular example the method accepts a Model and returns a view name as a String, but various other method parameters and return values can be used as explained later in this section. @Controller and @RequestMapping and a number of other annotations form the basis for the Spring MVC implementation.