spring中的@InitBinder和WebBindingInitializer接口

介绍

@InitBinder用于在@Controller中标注于方法,表示为当前控制器注册一个属性编辑器或者其他,只对当前的Controller有效。 @InitBinder必须有一个参数WebDataBinder

WebBindingInitializer

WebBindingInitializer:实现WebBindingInitializer,重写initBinder注册的属性编辑器是全局的属性编辑器,对所有的Controller都有效。

@InitBinder例子介绍

日期格式化

@Controller  
public class GlobalController {  


    @RequestMapping("test/{date}")  
    public void test(@PathVariable Date date, HttpServletResponse response) throws IOException  
        response.getWriter().write( date);
    }  

    @InitBinder//必须有一个参数WebDataBinder  
    public void initBinder(WebDataBinder binder) {  
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));  
    }    
}  
WebBindingInitializer 例子
public class MyWebBindingInitializer implements WebBindingInitializer {  

    @Override  
    public void initBinder(WebDataBinder binder, WebRequest request) {  
        // TODO Auto-generated method stub  
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));  
    }  

在springMVC的配置文件里面定义一个AnnotationMethodHandlerAdapter,并设置其WebBindingInitializer属性为我们自己定义的WebBindingInitializer对象

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
        <property name="cacheSeconds" value="0"/>  
        <property name="webBindingInitializer">  
            <bean class="com.xxx.blog.util.MyWebBindingInitializer"/>  
        </property>  
</bean>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值