springmvc中进行数据保存以及日期参数的保存过程解析
1.在Controller类中接受传入的日期类型的参数时
<form action="user/todate.do" method="post"> 日期:<input type="text" name="date"/><br /> <input type="submit" value="查看" /> </form>
@RequestMapping("todate.do") public String todate(Date date) { System.out.println(date); return "list"; } @InitBinder public void initBinder(ServletRequestDataBinder binder){ //只要网页中传来的数据格式为yyyy-MM-dd 就会转化为Date类型 binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true)); }
2.当要传入多个参数时
<form action="user/list2.do" method="post"> 姓名:<input type="text" name="uname"/><br> 密码:<input type="text" name="password"/><br> 性别:<input type="text" name="sex"/><br> 年龄:<input type="text" name="age"/><br> 地址:<input type="text" name="address"/><br> 生日:<input type="date" name="birth"><br> <input type="submit" value="提交"> </form>
@RequestMapping("list2.do") public String list2(Users users ) { System.out.println(users); return "list"; } @InitBinder public void initBinder(ServletRequestDataBinder binder){ //只要网页中传来的数据格式为yyyy-MM-dd 就会转化为Date类型 binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true)); }
Controller数据保存
保存至request里
(1)ModelAndView
@RequestMapping("aaa.do") public ModelAndView index() { ModelAndView mv = new ModelAndView(); mv.setViewName("index"); mv.addObject("name","张三"); return mv; }
(2)Model
@RequestMapping("aaa.do") public String index(Model model) { model.addAttribute("name", "李四"); return "index"; }
(3)map
@RequestMapping("aaa.do") public String index(Map<String, Object> map) { map.put("name", "222"); return "index"; }
(4)request
@RequestMapping("list.do") public String list(HttpServletRequest request) { request.setAttribute("name","wang"); return "index2"; }
保存至session里
@RequestMapping("list.do") public String list(HttpSession session) { session.setAttribute("name","wang"); return "index2"; }
保存至application里
@RequestMapping("list.do") public String list(HttpSession session) { session.getServletContext().setAttribute("name","wang"); return "index2"; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)