• SpringMVC初始化参数绑定--日期格式


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

    配置步骤:

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

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

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

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

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

    @Controller
    public class MyController {
    
    	//匹配单个
    	@InitBinder
    	public void initData(WebDataBinder wdb){
    		wdb.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    		
    	}
    	
    	
    	@RequestMapping(value="/first.do")
    	public String doFirst(Date birthday,int age){
    	
    		return "/welcome.jsp";
    	}
    }
    

    ③ 定制jsp页面:

     <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方法赋值

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

     

    实现效果:

     

  • 相关阅读:
    DevExpress GridControl使用方法
    DevExpress中,添加Winform窗体到DockPanel z
    取消默认 $ 定义
    五角星效果实现
    jquery的each函数的用法
    Object类型转换为long或者Long
    easyui datagrid 列排序
    redis.clients.jedis.exceptions.JedisDataException: WRONGTYPE Operation against a key holding the wrong kind of value
    DIV元素不换行
    JS 中div内容的显示和隐藏
  • 原文地址:https://www.cnblogs.com/WJ-163/p/6260146.html
Copyright © 2020-2023  润新知