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

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

 

实现效果:

 

 

 

posted @ 2017-01-07 19:15  Fighting`  阅读(4176)  评论(0编辑  收藏  举报