springmvc中进行数据保存以及日期参数的保存过程解析
1.在Controller类中接受传入的日期类型的参数时
日期:
@RequestMapping("todate.do") publicStringtodate(Datedate){ System.out.println(date); return"list"; } @InitBinder publicvoidinitBinder(ServletRequestDataBinderbinder){ //只要网页中传来的数据格式为yyyy-MM-dd就会转化为Date类型 binder.registerCustomEditor(Date.class,newCustomDateEditor(newSimpleDateFormat("yyyy-MM-dd"), true)); }
2.当要传入多个参数时
姓名:
密码:
性别:
年龄:
地址:
生日:
@RequestMapping("list2.do") publicStringlist2(Usersusers){ System.out.println(users); return"list"; } @InitBinder publicvoidinitBinder(ServletRequestDataBinderbinder){ //只要网页中传来的数据格式为yyyy-MM-dd就会转化为Date类型 binder.registerCustomEditor(Date.class,newCustomDateEditor(newSimpleDateFormat("yyyy-MM-dd"), true)); }
Controller数据保存
保存至request里
(1)ModelAndView
@RequestMapping("aaa.do") publicModelAndViewindex(){ ModelAndViewmv=newModelAndView(); mv.setViewName("index"); mv.addObject("name","张三"); returnmv; }
(2)Model
@RequestMapping("aaa.do") publicStringindex(Modelmodel){ model.addAttribute("name","李四"); return"index"; }
(3)map
@RequestMapping("aaa.do") publicStringindex(Mapmap){ map.put("name","222"); return"index"; }
(4)request
@RequestMapping("list.do") publicStringlist(HttpServletRequestrequest){ request.setAttribute("name","wang"); return"index2"; }
保存至session里
@RequestMapping("list.do") publicStringlist(HttpSessionsession){ session.setAttribute("name","wang"); return"index2"; }
保存至application里
@RequestMapping("list.do") publicStringlist(HttpSessionsession){ session.getServletContext().setAttribute("name","wang"); return"index2"; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。