SpringMVC:注解开发
@RequestMapping 使用
在SpringMVC中,RequestDispatcher(在Front Controller之下)这个servlet负责将进入的HTTP请求路由到控制器的处理方法,并通过RequestMapping注解指定请求与处理方法之间的映射关系。
@RequestMapping注解可以在Controller类及类中方法的级别上使用。
URL路径映射
@RequestMapping(value="/item")
或@RequestMapping("/item")
value的值是数组,可以将多个url映射到同一个方法,如:
@RestController
@RequestMapping("/home ")
public class IndexController {
@RequestMapping(value = {
" ",
"/page ",
"page* ",
"view/*,**/msg "
})
String indexMultipleMapping() {
return "Hello from index multiple mapping. ";
}
}
如下的这些 URL 都会由 indexMultipleMapping() 来处理:
- localhost:8080/home
- localhost:8080/home/
- localhost:8080/home/page
- localhost:8080/home/pageabc
- localhost:8080/home/view/
- localhost:8080/home/view/view
窄化请求映射
在class上添加@RequestMapping(url)指定通用请求前缀, 限制此类下的所有方法请求url必须以请求前缀开头,通过此方法对url进行分类管理。
@Controller
@RequestMapping("/items")
public class ItemsController {
@Autowired
private ItemsService itemsService;
// 商品查询
@RequestMapping("/queryItems")
public ModelAndView queryItems(HttpServletRequest request) throws Exception {
List<ItemsCustom> itemsList = itemsService.findItemsList(null);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemsList", itemsList);
modelAndView.setViewName("items/itemsList");
return modelAndView;
}
...
}
商品列表请求路径为:“/items/queryItems.action”。
限定请求方法
出于安全性考虑,对http的链接进行方法限制。
- HTTP 请求的方法:
RequestMethod.GET
、RequestMethod.POST
、RequestMethod.DELETE
、RequestMethod.PUT
、RequestMethod.PATCH
//商品信息修改页面显示
@RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
public ModelAndView editItems()throws Exception {
ItemsCustom itemsCustom = itemsService.findItemsById(1);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemsCustom", itemsCustom);
modelAndView.setViewName("items/editItems");
return modelAndView;
}
如果限制请求为post方法(method={RequestMethod.GET}),进行get请求,报错:
带有@RequestParam的RequestMapping
@RequestParam注解配合@RequestMapping一起使用,可以将请求的参数同处理方法的参数绑定在一起。
@RequestParam 的使用:
- 通过在@RequestParam里边指定request传入参数名称和形参进行绑定;
@RequestParam Integer items_id
:传入参数、形参均为items_id;@RequestParam(value="id") Integer items_id
:传入参数为id,形参为items_id;
- 通过required属性指定参数是否必须要传入;
@RequestParam(value="id",required=true) Integer items_id
- 通过defaultValue可以设置默认值,如果形参没有传入,将默认值和形参绑定;
@RequestParam(value="id",required=true,defaultValue="10001") Integer items_id
//商品信息修改页面显示
@RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
public String editItems(Model model,@RequestParam(value="id",required=true) Integer items_id)throws Exception {
ItemsCustom itemsCustom = itemsService.findItemsById(items_id);
//通过形参中的model将model数据传到页面
//相当于modelAndView.addObject方法
model.addAttribute("itemsCustom", itemsCustom);
return "items/editItems";
}
处理生产和消费对象
可以使用@RequestMapping注解的produces
和consumes
这两个元素来缩小请求映射类型的范围。
- 用@RequestMapping的
produces
元素结合@ResponseBody
注解,来用请求的媒体类型产生对象; - 用@RequestMapping的
comsumes
元素结合@RequestBody
注解,来用请求的媒体类型消费对象;
如:
@RestController
@RequestMapping("/home ")
public class IndexController {
@RequestMapping(value = "/prod ", produces = {"application/JSON "})
@ResponseBody
String getProduces() {
return "Produces attribute ";
}
@RequestMapping(value = "/cons ", consumes = {"application/JSON ","application/XML "})
@RequestBody
String getConsumes() {
return "Consumes attribute ";
}
}
在这段代码中,getProduces()处理方法会产生一个JSON响应,getConsumes()处理方法可以同时处理请求中的JSON和XML内容。
处理消息头
@RequestMapping注解提供了header
元素来根据请求中的消息头内容缩小请求映射的范围。
指定消息头,如:
@RestController
@RequestMapping("/home ")
public class IndexController {
@RequestMapping(value = "/head ", headers = {"content-type=text/plain"})
String post() {
return "Mapping applied along with headers ";
}
}
如上代码段, @RequestMapping注解的headers属性将映射范围缩小到了post()方法,使post()只会处理url为“/home/head”并且content-type为“text/plain”的请求。
指定多个消息头,如:
@RestController
@RequestMapping("/home ")
public class IndexController {
@RequestMapping(value = "/head ", headers = {
"content-type=text/plain ",
"content-type=text/html "
}) String post() {
return "Mapping applied along with headers ";
}
}
如上代码段, post()会处理url为“/home/head”,并且content-type为“text/plain”和“text/html”的请求。
处理请求参数
@RequestMapping 直接的params
元素可以进一步帮助我们缩小请求映射的定位范围。使用params
元素,可以让多个处理方法处理params不一样的同一个URL请求。
如:
@RestController
@RequestMapping("/home ")
public class IndexController {
@RequestMapping(value = "/fetch", params = {"id=10"})
String getParams(@RequestParam("personId") String id) {
return "Fetched parameter using params attribute = " + id;
}
@RequestMapping(value = "/fetch", params = {"personId=20"})
String getParamsDiff(@RequestParam("personId") String id) {
return "Fetched parameter using params attribute = " + id;
}
@RequestMapping(value = "/fetch", params = {"username","age!=10"})
public String getParamsDifferent() {
System.out.println("testParamsAndHeaders");
return SUCCESS;
}
}
如上代码段,getParams() 和 getParamsDifferent() 两个方法都能处理相同的一个 URL (“/home/fetch”) ,但是会根据 params 元素的配置不同而决定具体来执行哪一个方法:
- URL 为“/home/fetch?id=10”时,getParams() 会执行;
- URL 为“/home/fetch?personId=20”时,getParamsDiff() 会执行;
- URL 包含时“username”和“age”,且“age”不等于“10”时(如“/home/fetch?username=ejiux&age=26”),getParamsDifferent() 会执行;