一、@RequestMapping:地址映射。
@RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。
(1)用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
(2)用于方法上,表示方法中的响应请求都是以该地址作为路径。
二、@RequestBody和@ResponseBody
(1)@RequestBody
i) 该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对象上;
ii) 再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上。
(2)@ResponseBody
该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。
三、参数绑定注解:
(1)@PathVariable
当使用@RequestMapping地址映射时, 即 /edit/{id}, 这时的id可通过 @PathVariable注解绑定它传过来的值到方法的参数上。
@RequestMapping(KEY_EDIT + "/{id}")
public String edit(@PathVariable Integer id, ModelMap mm) {
Hand hand = service.findById(id);
mm.put("model", JsonKit.toJson(hand));
mm.put("id", id);
mm.put("code", CODE);
return BASE_PATH + "hand_edit.html";
}
(2)@RequestParam:获取参数。
在SpringMVC后台控制层,获取参数的方式主要有两种,一是通过request.getParameter("name")获取,二是使用注解@RequestParam直接获取。
//彻底删除
@ResponseBody
@RequestMapping(KEY_REMOVE)
public AjaxResult remove(@RequestParam String ids) {
int cnt = service.deleteByIds(ids);
if (cnt > 0) {
return success(DEL_SUCCESS_MSG);
} else {
return error(DEL_FAIL_MSG);
}
}
更多iOS、Android、Python、Java、MySQL的文章,请点击:
http://blog.csdn.net/jamiecheung
http://blog.csdn.net/u010841622