springmvc中進(jìn)行數(shù)據(jù)保存以及日期參數(shù)的保存過程解析
1.在Controller類中接受傳入的日期類型的參數(shù)時(shí)
<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){ //只要網(wǎng)頁(yè)中傳來的數(shù)據(jù)格式為yyyy-MM-dd 就會(huì)轉(zhuǎn)化為Date類型 binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true)); }
2.當(dāng)要傳入多個(gè)參數(shù)時(shí)
<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){ //只要網(wǎng)頁(yè)中傳來的數(shù)據(jù)格式為yyyy-MM-dd 就會(huì)轉(zhuǎn)化為Date類型 binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true)); }
Controller數(shù)據(jù)保存
保存至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"; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringMVC 傳日期參數(shù)到后臺(tái)的實(shí)例講解
- 解決springmvc關(guān)于前臺(tái)日期作為實(shí)體類對(duì)象參數(shù)類型轉(zhuǎn)換錯(cuò)誤的問題
- 解決SpringMVC接收不到ajaxPOST參數(shù)的問題
- 快速解決SpringMVC @RequestBody 用map接收請(qǐng)求參數(shù)的問題
- 解決SpringMVC Controller 接收頁(yè)面?zhèn)鬟f的中文參數(shù)出現(xiàn)亂碼的問題
- springmvc 傳遞和接收數(shù)組參數(shù)的實(shí)例
- springmvc接收參數(shù)為日期類型詳解
相關(guān)文章
Spring Boot 利用WebUploader進(jìn)行文件上傳功能
本文的重點(diǎn)是給大家介紹在Spring Boot項(xiàng)目中利用WebUploader如何進(jìn)行文件上傳,本文通過示例代碼給大家介紹,需要的朋友參考下吧2018-03-03Java利用EasyExcel解析動(dòng)態(tài)表頭及導(dǎo)出實(shí)現(xiàn)過程
以前做導(dǎo)出功能,表頭和數(shù)據(jù)都是固定的,下面這篇文章主要給大家介紹了關(guān)于Java利用EasyExcel解析動(dòng)態(tài)表頭及導(dǎo)出實(shí)現(xiàn)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12Google Kaptcha 框架實(shí)現(xiàn)登錄驗(yàn)證碼功能(SSM 和 SpringBoot)
這篇文章主要介紹了Google Kaptcha 實(shí)現(xiàn)登錄驗(yàn)證碼(SSM 和 SpringBoot)功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-12-12Java實(shí)現(xiàn)AOP功能的封裝與配置的小框架實(shí)例代碼
這篇文章主要介紹了Java實(shí)現(xiàn)AOP功能的封裝與配置的小框架實(shí)例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02