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
|
@Controllerpublic 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页面:
|
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方法赋值


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

实现效果:






欢迎提出意见或建议!!!

浙公网安备 33010602011771号