Spring相关注解使用总结
来源:互联网 发布:淘宝导航条黑色代码 编辑:程序博客网 时间:2024/06/05 15:52
@RequestMapping
SpringMVC会根据该注解后面跟的值去寻找目标方法。可以标注在类和方法上面,顺序为先类,再方法。如果类上不加次注解则直接根据方法上的注解去寻找。
- @RequestMapping(value = "xxx") <=> @RequestMapping("xxx");
- @RequestMapping({"list",""}):值为list或者为空,可以在方法中这样标注,这样路径进到该类中就会默认跳到该接口。
- @RequestMapping(value = "xxx", method = RequestMethod.GET),设置该接口的请求方式。
- 动态参数:@RequestMapping("xxx/{username}/yyy/{password}"),这样该接口请求的时候就必须在指定位置上填写相应的参数。该动态参数也可以用注解的方式去获取:@PathVariable("username"),该注解是要放在方法的入参里面的,这样在方法里面就可以直接拿“username”去使用。范例如下:
@Controllerpublic class TestController { @RequestMapping(value="/user/{userId}/roles/{roleId}",method = RequestMethod.GET) public String getLogin(@PathVariable("userId") String userId, @PathVariable("roleId") String roleId){ System.out.println("User Id : " + userId); System.out.println("Role Id : " + roleId); return "hello"; } @RequestMapping(value="/product/{productId}",method = RequestMethod.GET) public String getProduct(@PathVariable("productId") String productId){ System.out.println("Product Id : " + productId); return "hello"; } @RequestMapping(value="/javabeat/{regexp1:[a-z-]+}", method = RequestMethod.GET) public String getRegExp(@PathVariable("regexp1") String regexp1){ System.out.println("URI Part 1 : " + regexp1); return "hello"; }}
@ModelAttribute
此注解标注的方法会在所在类中每一个方法执行前执行。
常用情景:编辑表单。
@ModelAttributepublic AppInfo get(@RequestParam(required=false) String id) {if (StringUtils.isNotBlank(id)){return appInfoService.getAppInfo(id);}else{return new AppInfo();}}
@ModelAttributepublic void get(@RequestParam(required=false) String id, Map<String, Object> map) {AppInfo appInfo = null;if (StringUtils.isNotBlank(id)){appInfo = appInfoService.getAppInfo(id);}else{appInfo = new AppInfo();}map.put("appInfo", appInfo);}
@RequestMapping("form")public String addAppInfo(AppInfo appInfo, Model model){model.addAttribute("appInfo", appInfo);return "modules/appInfoForm";}
页面上点击删除、编辑的时候调用的是“form”这个接口,但是在调用该接口之前会调用@ModelAttribute标注的方法。
点击编辑按钮后台代码过程:
1、执行@ModelAttribute注解标注的方法,获取到对象。
2、把表单的请求参数赋给该对象对应的属性。
3、把上述对象传入目标方法的参数。
注意:注意对象命名的一致,其内在原因目前还不太清楚,要看源码,待补充。
参考链接:
http://jackyrong.iteye.com/blog/2059307
@RequestParam和@RequestBody
@RequestParam可以用于GET和POST请求:
GET:
@RequestMapping(value = "/saveUrl", method = RequestMethod.GET) public void toSaveRequest(@RequestParam(value = "url") String url, @RequestParam(value = "method") String method, HttpServletRequest request, HttpServletResponse response){ EtaSaveRequest savedRequest = new EtaSaveRequest(); savedRequest.setMethod(method); savedRequest.setRequestURI(url); authBeforeLoginDataStore.putSaveRequest(savedRequest, request, response); }POST:
@RequestMapping(value = "/saveUrl", method = RequestMethod.POST) public void toSaveRequest(@RequestParam(value = "url") String url, @RequestParam(value = "method") String method, HttpServletRequest request, HttpServletResponse response){ EtaSaveRequest savedRequest = new EtaSaveRequest(); savedRequest.setMethod(method); savedRequest.setRequestURI(url); authBeforeLoginDataStore.putSaveRequest(savedRequest, request, response); }
@RequestBody只能用于POST请求:
@Data public static class urlParam { @NotNull private String url; @NotNull private String method; } @RequestMapping(value = "/saveUrl", method = RequestMethod.POST) public void toSaveRequest(@RequestBody urlParam param, HttpServletRequest request, HttpServletResponse response){ EtaSaveRequest savedRequest = new EtaSaveRequest(); savedRequest.setMethod(param.getMethod()); savedRequest.setRequestURI(param.getUrl()); authBeforeLoginDataStore.putSaveRequest(savedRequest, request, response); }
$.ajax({ url: "/api/user/saveUrl", type: "POST", //用@RequestParam的时候POST请求URL如上 dataType: "json", contentType: "application/json", data: JSON.stringify({ "url":"/orgmanage/orgList", "method":"GET" }), success: function success(data) { console.log(data); location.href=data.result; } });
POST请求中,一般都作为一个对象传数据,声明Content-Type为application/json。
参考地址:
http://blog.csdn.net/xinluke/article/details/52710706
http://825635381.iteye.com/blog/2196911
@RestController和@Controller
@RestController == @Controller + @ResponseBody。
@Controller一般和SpringMVC中的视图解析器配合使用,根据前后缀匹配对应的跳转页面。
@RestController返回数据到前端,供前端进行解析,渲染页面。
参考链接:
http://www.cnblogs.com/softidea/p/5884772.html
http://blog.csdn.net/abap_brave/article/details/53005225
@ResponseBody
例如后台提供一个接口返回数据,例如一个list集合,前端ajax从此接口获取数据,那么该接口就要加上此注解,否侧获取不到数据,而是会得到XXXXXXX。
未完,待续。
0 0
- Spring相关注解使用总结
- Spring相关注解总结
- spring boot @ConditionalOnxxx相关注解总结
- spring boot @ConditionalOnxxx相关注解总结
- spring boot 的常用注解使用 总结
- spring boot 的常用注解使用 总结
- spring相关JUnit4注解
- Spring MVC相关注解
- spring相关注解
- spring aop相关注解
- spring 相关注解
- Spring注解@RequestBody相关
- spring相关注解
- Spring-mvc相关注解
- Spring 相关注解
- Spring 注解总结
- Spring 注解总结
- Spring 注解总结
- js如何用正则表达式验证手机号码,邮箱,座机号
- Java类初始化顺序
- 课堂笔记
- Android补间动画卡顿
- 人员选择树,搜索自动筛选功能
- Spring相关注解使用总结
- 计算机系统知识
- 对象的肤浅认识和工厂模式
- JavaScript ondragstart、ondragenter、ondragover 、ondrop、ondragend
- POJ 2914 无向图的最小割,模板题。
- 前端面试整理
- 排序算法总结
- HashMap和Hashtable的区别
- cocos2d-x 3.3 中各种动画笔记与实例