自定义参数绑定使用属性编辑器

1、PropertyEditor

使用WebDataBinder

在controller中通过@InitBinder标识方法为参数绑定方法,通过WebDataBinder注册属性编辑器,问题是此方法只能在单个controller类中注册。

@InitBinder
	public void initBinder(WebDataBinder binder) throws Exception {
		binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
	}

2、WebBindingInitializer

 自定义WebBindingIntializer,注入到处理器适配器中。

编写CustomPropertyEditor实现PropertyEditorRegistrar接口:

public class CustomPropertyEditor implements PropertyEditorRegistrar {

	@Override
	public void registerCustomEditors(PropertyEditorRegistry binder) {
		binder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));	
	}
}

配置文件:












	
 


 

你可能感兴趣的:(ssm)