這篇文章主要介紹“springmvc中怎么對數據進行保存以及日期參數的保存過程解析”,在日常操作中,相信很多人在springmvc中怎么對數據進行保存以及日期參數的保存過程解析問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”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";
}到此,關于“springmvc中怎么對數據進行保存以及日期參數的保存過程解析”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。