`
yeak2001
  • 浏览: 101415 次
  • 性别: Icon_minigender_1
  • 来自: 无锡
社区版块
存档分类
最新评论

@InitBinder

阅读更多
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.
@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
@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进去
分享到:
评论
2 楼 yeak2001 2012-06-27  
傲视温柔 写道
请问一下
@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)); 
    } 

直接写完这个类就可以绑定数据了吗 , 我这里不行啊,老报错,是不是在其他地方还得有点配置或者操作什么的


<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer" ref="commonBindingInitializer" />
</bean>
1 楼 傲视温柔 2012-05-30  
请问一下
@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)); 
    } 

直接写完这个类就可以绑定数据了吗 , 我这里不行啊,老报错,是不是在其他地方还得有点配置或者操作什么的

相关推荐

    详解SpringMVC注解@initbinder解决类型转换问题

    本篇文章主要介绍了详解SpringMVC注解@initbinder解决类型转换问题,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    SpringMVC中利用@InitBinder来对页面数据进行解析绑定的方法

    本篇文章主要介绍了SpringMVC中利用@InitBinder来对页面数据进行解析绑定的方法,非常具有实用价值,需要的朋友可以参考下

    SpringMVC的@InitBinder参数转换代码实例

    主要介绍了SpringMVC的@InitBinder参数转换代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    spring mvc使用@InitBinder标签对表单数据绑定的方法

    主要介绍了spring mvc使用@InitBinder标签对表单数据绑定的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    Spring MVC InitBinder验证方法

    主要介绍了Spring MVC InitBinder验证方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    springMVC详解以及注解说明

    注解介绍等详细说明及使用: • @Controller • @Service • @Autowired • @RequestMapping • @RequestParam • @ModelAttribute • @Cacheable • @CacheFlush • @Resource ... • @InitBinder

    SpringMVC自定义属性编辑器详解及实例

    自定义springMVC的属性编辑器主要有两种方式,一种是使用@InitBinder标签在运行期注册一个属性编辑器,这种编辑器只在当前Controller里面有效;还有一种是实现自己的 WebBindingInitializer,然后定义一个...

    springboot学习思维笔记.xmind

    @InitBinder @ModelAttribute 其他配置 ViewController 路径匹配参数配置 WebMvcConfigurerAdapter WebMvcConfigurer Spring MVC的高级配置 文件上传配置 自定义...

    27 Spring MVC数据绑定InitBinder揭秘慕课专栏1

    背景在使用 SpingMVC 框架的项目中,经常会遇到页面某些数据要转换成类型是 Date、Integer、Double 等的数据绑定到控制器的实体。Sprin

    XSS防攻击实现

    XSS防攻击实现,是否为忽略xss拦截 默认为false,转义字符,InitBinder

    SpringMVC示例

    员工信息、RESTRUL_CRUD_添加操作&表单标签、RESTRUL_CRUD_删除操作&处理静态资源、RESTRUL_CRUD_修改操作、自定义类型转换器、annotation-driven配置、InitBinder注解、数据的格式化、JSR 303数据校验、错误消息的...

    SpringMVC Employee Demo

    driven配置、InitBinder注解、数据的格式化、JSR303数据校验、错误消息的显示及国际化、Ajax返回JSON、使用HttpMessageConverter、国际化_通过超链接切换中英文、文件上传、自定义的拦截器、拦截器的零Xml配置、异常...

    spring-web-2.5.jar

    org.springframework.web.bind.annotation.InitBinder.class org.springframework.web.bind.annotation.ModelAttribute.class org.springframework.web.bind.annotation.RequestMapping.class org.springframework....

Global site tag (gtag.js) - Google Analytics