简体   繁体   English

spring MVC中init绑定器的用途是什么

[英]What is the purpose of init binder in spring MVC

This is the code on internet for init binder 这是针对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 is it used, I mean, what was the problem before , how it was solved with that function. 1)为什么使用它,我的意思是,之前的问题是什么,它是如何通过该功能解决的。 so i want to know what was the problem with orginal date which was solved with this date format? 所以我想知道这个日期格式解决的原始日期有什么问题?

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? 2)如何从JSP表单的角度使用这种格式,我的意思是,如果我们以文本格式输入日期,它是否会转换为特定格式然后保存它?

3) How does it apply that formatting , I mean , do we have to do something in object class ? 3)它是如何应用格式化的,我的意思是,我们必须在对象类中做一些事情吗?

1) Before, you had to resort to manually parsing the date: 1)之前,您不得不求助于手动解析日期:

 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. 2)如果您的jsp页面在表单yyyy-MM-dd上提供日期,您可以直接在控制器中将其作为Date对象检索。

3) Spring tries against all registered editors to see if values can be converted into objects. 3)Spring尝试对所有注册的编辑器查看是否可以将值转换为对象。 You don't have to do anything in the object itself, that's the beauty of it. 你不必在对象本身做任何事情,这就是它的美妙之处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM