zoukankan      html  css  js  c++  java
  • Spring日期格式初始化

    原文链接地址

    一、初始化参数绑定[一种日期格式]

    配置步骤:

    ①:在applicationcontext.xml中只需要配置一个包扫描器即可

    1
    2
    <!-- 包扫描器 -->
         <context:component-scan base-package="cn.happy.controller"></context:component-scan>

    ②:在处理器类中配置绑定方法  使用@InitBinder注解

    在这里首先注册一个用户编辑器 参数一为目标类型   propertyEditor为属性编辑器,此处我们选用 CustomDateEditor属性编辑器,

    参数一为想转换的日期格式,参数二表示是否允许为空

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    @Controller
    public class MyController {
     
        //匹配单个
        @InitBinder
        public void initData(WebDataBinder wdb){
            wdb.registerCustomEditor(Date.classnew CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
             
        }
         
         
        @RequestMapping(value="/first.do")
        public String doFirst(Date birthday,int age){
         
            return "/welcome.jsp";
        }
    }

    ③ 定制jsp页面:

    1
    2
    3
    4
    5
    6
    <form action="${pageContext.request.contextPath }/first.do" method="post">
       <h1>参数绑定转换器</h1>
                    出生日期:<input name="birthday" value="${birthday}"/><br/><br/>
                    年龄:<input name="age" value="${age }"/><br/><br/>
          <input type="submit" value="注册"/>
       </form>

    实现效果:


    二、多日期的绑定

    ①自定义的属性编辑器,需要我们继承PropertiesEditor,重写里面的setAsText方法,使用setValue方法赋值

    ②在处理器类中使用我们自定的属性编辑器

     

    实现效果:

     

  • 相关阅读:
    接口运用实例
    C# Lambda表达式运用
    图片转换图片流方法(二进制流)
    简单的winform编辑器
    C# OO(初级思想)。
    MVC知识点
    提高sql查询效率
    DataRead 和DataSet区别
    JavaScript内置对象与原生对象【转】
    Cookie,Sesstion,Application 缓存。
  • 原文地址:https://www.cnblogs.com/gaofei-1/p/7473996.html
Copyright © 2011-2022 走看看