• 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";
        }

     以下网页可供参考:https://www.cnblogs.com/gflb/p/11172621.html

  • 相关阅读:
    HDU_1846 Brave Game(sg函数简化版)
    POJ 3225 Help with Intervals(线段树)
    ZOJ_3501 Roman Order
    HDU_1527 取石子游戏(威佐夫博弈)
    HDU_2112 HDU Today(DIjkstra + map映射)
    HDU_3339 In Action(Dijkstra + DP)
    单链表的实现(课本上的算法)
    haproxy 基于 uri 实现调度
    haproxy 基于 cookie 实现 session 绑定
    [转]ASP.NET 虚拟主机安全漏洞解决方案
  • 原文地址:https://www.cnblogs.com/yufengwang/p/11456227.html
Copyright © 2020-2023  润新知