@InitBinder
来源:互联网 发布:张大奕的淘宝店叫啥 编辑:程序博客网 时间:2024/05/17 21:43
Customizing WebDataBinder initialization
To customize request parameter binding with PropertyEditors, etc. via Spring's WebDataBinder, you can either use @InitBinder-annotated methods within your controller or externalize your configuration by providing a custom WebBindingInitializer.
Customizing data binding with @InitBinder
Annotating controller methods with @InitBinder allows you to configure web data binding directly within your controller class. @InitBinder identifies methods which initialize the WebDataBinder which will be used for populating command and form object arguments of annotated handler methods.
Such init-binder methods support all arguments that @RequestMapping supports, except for command/form objects and corresponding validation result objects. Init-binder methods must not have a return value. Thus, they are usually declared as void. Typical arguments include WebDataBinder in combination with WebRequest or java.util.Locale, allowing code to register context-specific editors.
The following example demonstrates the use of @InitBinder for configuring a CustomDateEditor for all java.util.Date form properties.
WebDataBinder是用来绑定请求参数到指定的属性编辑器,可以继承WebBindingInitializer来实现一个全部controller共享的dataBiner
也可以在某个单独的contorller里定义一个dataBinder,使用@InitBinder注解就可以实现
PS:WebDataBinder是用来绑定请求参数到指定的属性编辑器.由于前台传到controller里的值是String类型的,当往Model里Set这个值的时候,如果set的这个属性是个对象,Spring就会去找到对应的EDITOR进行转换,然后再SET进去
To customize request parameter binding with PropertyEditors, etc. via Spring's WebDataBinder, you can either use @InitBinder-annotated methods within your controller or externalize your configuration by providing a custom WebBindingInitializer.
Customizing data binding with @InitBinder
Annotating controller methods with @InitBinder allows you to configure web data binding directly within your controller class. @InitBinder identifies methods which initialize the WebDataBinder which will be used for populating command and form object arguments of annotated handler methods.
Such init-binder methods support all arguments that @RequestMapping supports, except for command/form objects and corresponding validation result objects. Init-binder methods must not have a return value. Thus, they are usually declared as void. Typical arguments include WebDataBinder in combination with WebRequest or java.util.Locale, allowing code to register context-specific editors.
The following example demonstrates the use of @InitBinder for configuring a CustomDateEditor for all java.util.Date form properties.
Java代码
- @Controller
- public class MyFormController {
- @InitBinder
- public void initBinder(WebDataBinder binder) {
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
- dateFormat.setLenient(false);
- binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
- }
- // ...
- }
WebDataBinder是用来绑定请求参数到指定的属性编辑器,可以继承WebBindingInitializer来实现一个全部controller共享的dataBiner
Java代码
- @Component
- public class CommonBindingInitializer implements WebBindingInitializer {
- public void initBinder(WebDataBinder binder, WebRequest request) {
- SimpleDateFormat dateFormat = new SimpleDateFormat(ERPUtil.ISO_DATE_MASK);
- dateFormat.setLenient(false);
- binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
- binder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
- }
- }
也可以在某个单独的contorller里定义一个dataBinder,使用@InitBinder注解就可以实现
PS:WebDataBinder是用来绑定请求参数到指定的属性编辑器.由于前台传到controller里的值是String类型的,当往Model里Set这个值的时候,如果set的这个属性是个对象,Spring就会去找到对应的EDITOR进行转换,然后再SET进去
0 0
- @InitBinder
- @initBinder
- @initBinder
- @InitBinder
- @InitBinder注解
- @InitBinder的使用
- springMVC注解@initbinder
- spring mvc @InitBinder
- SpringMVC-08 @InitBinder注解
- springMVC注解@initbinder
- initBinder,对日期修改
- springMVC @initBinder 使用
- springmvc中@InitBinder
- spring mvc @InitBinder
- springMVC注解@initbinder
- @InitBinder的作用
- springMvc中的@InitBinder
- SpringMVC 中 @InitBinder
- 支持向量机SVM中的对偶问题
- Android 判断网络类型
- Valid Parentheses
- 【Unity3D自学记录】NGUI之UIButtonMessage长按
- 9月24日
- @InitBinder
- 黑马入学_学习blog_第六篇
- Linux的进程/线程间通信方式总结
- hdu 5023 A Corrupt Mayor's Performance Art (线段树+区间更新+状压)
- Nginx源码分析1
- 排序方法
- 【设计模式】使用设计模式来解决设计问题
- org.apache.commons.beanutils.ConversionException: No value specified for 'Date'
- Android基于SOAP标准调用Webservice实现数据交互