首页 > 代码库 > Spring MVC控制器
Spring MVC控制器
Spring MVC中控制器用于解析用户请求并且转换为模型,提供访问应用程序的行为,通常用注解方式实现.
org.springframework.stereotype.Controller注解类型用于声明Spring类的实例为一个控制器, Spring可以扫描找到应用程序中所有基于注解的控制器类,所以需要在配置文件中声明扫描路径:
<!-- 自动扫描包,实现支持注解的IOC --> <context:component-scan base-package="cn.luan.controller" />
一个简单的控制器:
@Controller public class BarController { //映射访问路径 @RequestMapping("/index") public String index(Model model){ //Spring MVC会自动实例化一个Model对象用于向视图中传值 model.addAttribute("message", "这是通过注解定义的一个控制器中的Action"); //返回视图位置 return "foo/index"; } }
@RequestMapping注解用于映射url到控制器类或一个方法。可用于类和方法上。该注解共有8个属性:
public @interface RequestMapping { java.lang.String name() default ""; @org.springframework.core.annotation.AliasFor("path") java.lang.String[] value() default {}; @org.springframework.core.annotation.AliasFor("value") java.lang.String[] path() default {}; org.springframework.web.bind.annotation.RequestMethod[] method() default {}; java.lang.String[] params() default {}; java.lang.String[] headers() default {}; java.lang.String[] consumes() default {}; java.lang.String[] produces() default {}; }
value和path属性功能相同, 用来指定映射路径或URL模板,数组,允许@RequestMapping(value=http://www.mamicode.com/{"/rm1","/rm2"})。
name属性指定映射器名称,一般情况下不指定
method属性指定请求方式,可以为GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE,用来过滤请求范围,不符合的请求将返回405:Method Not Allowed
params属性指定映射参数规则
consumes属性指定请求的内容类型,如application/json, text/html
produces属性指定返回的内容类型,如application/json; charset=UTF-8
headers属性指定映射请求头部,如Host,Content-Type等
一个例子:
@Controller @RequestMapping("/appointments") public class AppointmentsController { private final AppointmentBook appointmentBook; @Autowired public AppointmentsController(AppointmentBook appointmentBook) { this.appointmentBook = appointmentBook; } @RequestMapping(method = RequestMethod.GET) public Map<String, Appointment> get() { return appointmentBook.getAppointmentsForToday(); } @RequestMapping(value = "/{day}", method = RequestMethod.GET) public Map<String, Appointment> getForDay(@PathVariable @DateTimeFormat(iso = ISO.DATE) Date day, Model model) { return appointmentBook.getAppointmentsForDay(day); } @RequestMapping(value = "/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"; } }
value属性只注解方法,访问路径:http://localhost:8080/springmvctest/act
@Controller public class myController { @RequestMapping("/act") public String act(){ return "jsp/index"; } }
value属性同时注解类和方法,访问路径:http://localhost:8080/springmvctest/my/act
@Controller @RequestMapping("/my") public class MyController { @RequestMapping("/act") public String act(){ return "jsp/index"; } }
value属性默认为空,所以允许@RequestMapping, 此时表示该类或方法为默认控制器,默认方法。如下action,访问路径为:http://localhost:8080/springmvctest/
@Controller @RequestMapping //默认控制器 public class myController { @RequestMapping //默认action public String action(Model model){ model.addAttribute("message", "action2"); return "jsp/index"; } }
Spring MVC可以使用@PathVariable 注释方法参数的值绑定到一个URL模板变量,访问路径:http://localhost:8080/springmvctest/action/1/2, 参数类型必须一致,否则找不到路径。
@RequestMapping("/action/{p1}/{p2}") public String action(@PathVariable int p1,@PathVariable int p2,Model model){ model.addAttribute("message", p1+" "+p2);
return "jsp/index"; }
支持正则表达式,访问路径:http://localhost:8080/springmvctest/action/1234-abcd
@RequestMapping(value="http://www.mamicode.com/action/{id://d{4}}-{name:[a-z]{4}}") public String action(@PathVariable int id,@PathVariable String name,Model model){ model.addAttribute("message", "id:"+id+" name:"+name); return "jsp/index"; }
支持通配符,访问路径:http://localhost:8080/springmvctest/action/test1.do
@RequestMapping(value = "http://www.mamicode.com/action/*.do") public String action(Model model){ model.addAttribute("message","123"); return "jsp/index"; }
过滤提交的内容类型
@Controller @RequestMapping("/my") public class MyController { // 请求内容类型必须为text/html,浏览器默认没有指定Content-type @RequestMapping(value = "http://www.mamicode.com/action",consumes="text/html") public String action(Model model) { model.addAttribute("message", "请求的提交内容类型是text/html"); return "jsp/index"; } }
指定返回的内容类型
@RequestMapping(value = "http://www.mamicode.com/action",produces="application/json; charset=UTF-8") public String action(Model model) { model.addAttribute("message", "application/json; charset=UTF-8"); return "jsp/index"; }
过滤映射请求的参数,限制客户端发送到服务器的请求参数为某些特定值或不为某些值,访问路径:http://localhost:8080/springdemo/show/action?id=10&name=root
@RequestMapping(value = "http://www.mamicode.com/action",params={"id!=0","name=root"}) public String action(Model model) { model.addAttribute("message", ""); return "jsp/index"; }
过滤映射请求头部,约束客户端发送的请求头部信息中必须包含某个特定的值或不包含某个值,作用范围大于consumes 和 produces
@RequestMapping(value = "http://www.mamicode.com/action",headers={"Host=localhost:8088",Content-Type="application/*"}) public String action(Model model) { model.addAttribute("message", ""); return "jsp/index"; }
end
Spring MVC控制器