深入SpringMVC组件开发【1】
来源:互联网 发布:冰点下载器 mac版 编辑:程序博客网 时间:2024/05/29 16:12
一、控制器接收各类请求参数
1.接收普通请求参数
/*接收普通参数 */@RequestMapping("/commonParams")public ModelAndView commonParams(String roleName, String note) {System.out.println("roleName =>" + roleName);System.out.println("note =>" + note);ModelAndView mv = new ModelAndView();mv.setViewName("index");return mv;}/*接收对象 */@RequestMapping("/commonParamPojo")public ModelAndView commonParamPojo(RoleParams roleParams) {System.out.println("roleName =>" + roleParams.getRoleName());System.out.println("note =>" + roleParams.getNote());ModelAndView mv = new ModelAndView();mv.setViewName("index");return mv;}
2.使用@RequestParam注解获取参数
/*使用@RequestParam("role_name")指定映射HTTP参数名称 */@RequestMapping("/requestParam")public ModelAndView requestParam(@RequestParam("role_name") String roleName, String note) {System.out.println("roleName =>" + roleName);System.out.println("note =>" + note);ModelAndView mv = new ModelAndView();mv.setViewName("index");return mv;}
3.使用URL传递参数
/*使用URL传递参数{id}代表接收一个参数注解@PathVariable表示从URL的请求地址中获取参数 */@RequestMapping("/getRole/{id}")public ModelAndView pathVariable(@PathVariable("id") Long id) {Role role = roleService.getRole(id);ModelAndView mv = new ModelAndView();//绑定数据模型mv.addObject(role);//设置为JSON视图mv.setView(new MappingJackson2JsonView());return mv;}
4.传递JSON参数
@RequestMapping("/findRoles")public ModelAndView findRoles(@RequestBody RoleParams roleParams) {List<Role> roleList = roleService.findRoles(roleParams);ModelAndView mv = new ModelAndView();//绑定模型mv.addObject(roleList);//设置为JSON视图mv.setView(new MappingJackson2JsonView());return mv;}
5.接收列表数据和表单序列化
/* var idList = [1, 2, 3]; data: JSON.stringify(idList), */@RequestMapping("/deleteRoles")public ModelAndView deleteRoles(@RequestBody List<Long> idList) {ModelAndView mv = new ModelAndView();//删除角色int total = roleService.deleteRoles(idList);//绑定视图mv.addObject("total", total);//JSON视图mv.setView(new MappingJackson2JsonView());return mv;}/*var roleList = [{roleName: 'role_name_1', note: 'note_1'},{roleName: 'role_name_2', note: 'note_2'},{roleName: 'role_name_3', note: 'note_3'} ]; data: JSON.stringify(roleList), */@RequestMapping("/addRoles")public ModelAndView addRoles(@RequestBody List<Role> roleList) {ModelAndView mv = new ModelAndView();//新增角色int total = roleService.insertRoles(roleList);//绑定视图mv.addObject("total", total);//JSON视图mv.setView(new MappingJackson2JsonView());return mv;}/*序列化表单data: $("form").serialize(), */@RequestMapping("/commonParamPojo2")public ModelAndView commonParamPojo2(String roleName, String note) {System.out.println("roleName =>" + roleName);System.out.println("note =>" + note);ModelAndView mv = new ModelAndView();mv.setViewName("index");return mv;}
二、重定向
1.简单重定向
@RequestMapping("/showRoleJsonInfo")public ModelAndView showRoleJsonInfo(Long id, String roleName, String note) { ModelAndView mv = new ModelAndView(); mv.setView(new MappingJackson2JsonView()); mv.addObject("id", id); mv.addObject("roleName", roleName); mv.addObject("note", note); return mv;}/* Model为重定向数据模型,Spring MVC会自动初始化它 */@RequestMapping("/addRole")public String addRole(Model model, String roleName, String note) { Role role = new Role(); role.setRoleName(roleName); role.setNote(note); //插入角色后,会回填角色编号 roleService.insertRole(role); //绑定重定向数据模型 model.addAttribute("roleName", roleName); model.addAttribute("note", note); model.addAttribute("id", role.getId()); return "redirect:./showRoleJsonInfo.do";}/* 通过ModelAndView实现重定向*/@RequestMapping("/addRole2")public ModelAndView addRole(ModelAndView mv, String roleName, String note) { Role role = new Role(); role.setRoleName(roleName); role.setNote(note); //插入角色后,会回填角色编号 roleService.insertRole(role); //绑定重定向数据模型 mv.addObject("roleName", roleName); mv.addObject("note", note); mv.addObject("id", role.getId()); mv.setViewName("redirect:./showRoleJsonInfo.do"); return mv;}
2.重定向传递POJO对象
/* 重定向-通过POJO传递对象 */ @RequestMapping("/showRoleJsonInfo2") public ModelAndView showRoleJsonInfo(Role role) { ModelAndView mv = new ModelAndView(); mv.setView(new MappingJackson2JsonView()); mv.addObject("role", role); return mv; } /* 重定向传递POJO RedirectAttributes */ @RequestMapping("/addRole3") //RedirectAttribute对象Spring MVC会自动初始化它 public String addRole3(RedirectAttributes ra, Role role) { //插入角色后,会回填角色编号 roleService.insertRole(role); //绑定重定向数据模型 ra.addFlashAttribute("role", role); return "redirect:./showRoleJsonInfo2.do"; }
三、保存并获取属性参数
1.注解@RequestAttribute
<html> <body> <% //设置请求属性 request.setAttribute("id", 1L); //转发给控制器 request.getRequestDispatcher("./attribute/requestAttribute.do").forward(request, response); %> </body></html>
@RequestMapping("/requestAttribute")public ModelAndView reqAttr(@RequestAttribute(name = "id", required = false) Long id){ ModelAndView mv = new ModelAndView(); Role role = roleService.getRole(id); mv.addObject("role", role); mv.setView(new MappingJackson2JsonView()); return mv;}
2.注解@SessionAttribute和注解@SessionAttributes
@SessionAttributes只能对类进行标注,设置session属性
@SessionAttribute获取session属性
@Controller@RequestMapping("/attribute")//可以配置数据模型的名称和类型,两者取或关系@SessionAttributes(names = "id", types = {Role.class})public class AttributeController { @Autowired private RoleService roleService; @RequestMapping("/sessionAttributes") public ModelAndView sessionAttrs(Long id) { ModelAndView mv = new ModelAndView(); Role role = roleService.getRole(id); //根据类型,session将会保存角色信息 mv.addObject("role", role); //根据名称,session将会保存id mv.addObject("id", id); //视图名称,定义跳转到一个JSP文件上 mv.setViewName("sessionAttribute"); return mv; }}
<html> <body> <% Role role = (Role) session.getAttribute("role"); out.println("id = " + role.getId() + "<p/>"); out.println("roleName = " + role.getRoleName() + "<p/>"); out.println("note = " + role.getNote() + "<p/>"); Long id = (Long) session.getAttribute("id"); out.println("id = " + id + "<p/>"); %> </body></html>
<html> <body> <% //设置Session属性 session.setAttribute("id", 1L); //执行跳转 response.sendRedirect("./attribute/sessionAttribute.do"); %> </body></html>
@RequestMapping("/sessionAttribute")public ModelAndView sessionAttr(@SessionAttribute(name = "id", required = false) Long id) { ModelAndView mv = new ModelAndView(); Role role = roleService.getRole(id); mv.addObject("role", role); mv.setView(new MappingJackson2JsonView()); return mv;}
3.注解@CookieValue和注解@RequestHeader
@RequestMapping("/getHeaderAndCookie")public String testHeaderAndCookie( @RequestHeader(value = "User-Agent", required = false, defaultValue = "attribute") String userAgent, @CookieValue(value = "JSESSIONID", required = true, defaultValue = "MyJsessionid") String jsessionId){ System.out.println("User-Agent:" + userAgent); System.out.println("JSESSIONID:" + jsessionId); return "index";}
阅读全文
0 0
- 深入SpringMVC组件开发【1】
- springmvc深入1
- springMVC组件
- 深入springmvc
- 深入SpringMVC
- 深入springMVC
- 深入SpringMVC
- COM组件开发 1
- 【Spring+SpringMVC+MyBatis深入学习及搭建】15.SpringMVC注解开发(基础篇)
- 【Spring+SpringMVC+MyBatis深入学习及搭建】16.SpringMVC注解开发(高级篇)
- SpringMVC结构---核心组件
- SpringMVC核心组件
- SpringMVC 组件类大全
- springmvc组件总结
- SpringMVC 组件类大全
- SpringMVC 组件类大全
- SpringMVC--核心组件
- springmvc组件详解
- QT控件大全 四十一 QLightBoxWidget
- spring cloud+es+kafka+zipkin windows环境搭建调用链监控
- 用一条查询SQL统计某个字段各字符及出现的次数,并按出现次数倒序排列。
- serialVersionUID作用
- 双十二年终盛典,EasyRecovery与您一起
- 深入SpringMVC组件开发【1】
- 融云成为“2017年度CIO最信赖供应商” 加速企业IM能力升级
- 机器学习竞赛技巧
- JAVA代理那些事儿
- git-flow使用
- Java【集合系列】-04- fail-fast总结(通过ArrayList来说明fail-fast的原理、解决办法)
- hibernate session开启与数据库物理连接的时间关系
- Spring Boot企业微信点餐系统
- 如何高效产生m个n范围内的不重复随机数(m<=n)