带注释的控制器

Spring MVC 提供了一个基于 Comments 的编程模型,其中@Controller@RestController组件使用注解来表示请求映射、请求输入、 异常处理等。带 Comments 的控制器具有灵活的方法签名和 不必扩展基类,也不必实现特定的接口。 以下示例显示了由 annotations 定义的控制器:

@Controller
public class HelloController {

	@GetMapping("/hello")
	public String handle(Model model) {
		model.addAttribute("message", "Hello World!");
		return "index";
	}
}

In the preceding example, the method accepts a Model and returns a view name as a String, but many other options exist and are explained later in this chapter.

Guides and tutorials on spring.io use the annotation-based programming model described in this section.

APP信息