spring MVC中init binder的目的是什么 [英] What is the purpose of init binder in spring MVC

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

问题描述

这是互联网上用于init binder的代码

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));
}

谁能解释一下:

1) 为什么使用它,我的意思是,之前的问题是什么,它是如何使用该功能解决的.所以我想知道用这种日期格式解决的原始日期有什么问题?

1) Why is it used, I mean, 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?

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

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) 它是如何应用这种格式的,我的意思是,我们是否必须在对象类中做一些事情?

3) How does it apply that formatting , I mean , do we have to do something in object class ?

推荐答案

1) 之前,您不得不求助于手动解析日期:

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) 如果您的 jsp 页面在表单 yyyy-MM-dd 上提供日期,您可以直接将其作为 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 对所有已注册的编辑器进行尝试,以查看值是否可以转换为对象.您不必在对象本身中做任何事情,这就是它的美妙之处.

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.

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

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