什么是INIT粘结剂Spring MVC中的目的 [英] What is the purpose of init binder in spring MVC

查看:105
本文介绍了什么是INIT粘结剂Spring MVC中的目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是互联网上的code init的粘合剂

  @InitBinder
    公共无效initBinder(WebDataBinder粘合剂){
        SimpleDateFormat的日期格式=新的SimpleDateFormat(YYYY-MM-DD);        binder.registerCustomEditor(Date.class,新和CustomDateEditor(日期格式,真));
    }

任何人都可以请解释


  

1)为什么用它我的意思是说什么
  以前是问题,究竟是怎么回事
  与该功能解决。所以我想
  知道是什么的问题
  将其用解决原单日期
  此日期格式米


  
  

2)如何使用这种格式从JSP
  形式着眼点我的意思是,如果我们进入
  日期文本格式,它隐蔽
  以特定的格式,然后将其保存


  
  

3)它是如何应用该formsatting,
  我的意思是,做我们必须做的事
  对象类



解决方案

1)之前,你不得不求助于人工解析日期:

 公共无效的WebMethod(@RequestParam(DATE)字符串strDate){
    日期日期= ... //手动解析日期
 }

现在你可以直接得到解析日期:

 公共无效的WebMethod(@RequestParam(DATE)日期为准){
 }

2)如果你的 JSP 页表格上提供了一个日期 YYYY-MM-DD ,你可以找回作为日期对象直接在您的控制器。

3)弹簧尝试对所有注册的编辑,查看是否值可以转换成对象。您不必在对象本身做任何事,这就是它的美丽。

This is the code on internet for init binder

@InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

Can anyone please explain

1)Why it is used i mean to say what was the problem before , how it was solved with that function. so i want to know what was the problem with orginal date which was solved with this date format m.

2)How to use this format from the JSP form point of view i mean if we enter date in text format , does it covert to specific format and then save it

3)How does it apply that formsatting , i mean , do we have to do something in object class

解决方案

1) Before, you had to resort to manually parsing the date:

 public void webmethod(@RequestParam("date") String strDate) {
    Date date = ... // manually parse the date
 }

Now you can get the parsed date directly:

 public void webmethod(@RequestParam("date") Date date) {
 }

2) If your jsp page supplies a date on the form yyyy-MM-dd you can retrieve it as a Date object directly in your controller.

3) Spring tries against all registered editors to see if values can be converted into objects. You don't have to do anything in the object itself, that's the beauty of it.

这篇关于什么是INIT粘结剂Spring MVC中的目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆