Spring MVC - 绑定日期字段 [英] Spring MVC - Binding a Date Field

查看:26
本文介绍了Spring MVC - 绑定日期字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于表示字符串、数字和布尔值的请求参数,Spring MVC 容器可以将它们绑定到开箱即用的类型化属性.

你如何让 Spring MVC 容器绑定一个表示日期的请求参数?

说到这里,Spring MVC 是如何判断给定请求参数的类型的?

谢谢!

解决方案

Spring MVC 如何确定给定请求参数的类型?

Spring 使用了 ServletRequestDataBinder 绑定它的值.该过程可以描述如下

/*** 捆绑模拟请求*/MockHttpServletRequest request = new MockHttpServletRequest();request.addParameter("name", "Tom");request.addParameter("age", "25");/*** Spring 在处理请求之前创建一个新的命令对象** 通过调用<COMMAND_CLASS>.class.newInstance();*/人人 = 新人();

...

/*** 然后用 ServletRequestDataBinder 绑定提交的值** 它利用Java反射来绑定其值*/ServletRequestDataBinder binder = new ServletRequestDataBinder(person);活页夹(请求);

在幕后,DataBinder 实例在内部使用 BeanWrapperImpl 实例负责设置命令对象的值.随着 getPropertyType 方法,它检索属性类型

如果你看到上面提交的请求(当然,通过使用模拟),Spring会调用

BeanWrapperImpl beanWrapper = new BeanWrapperImpl(person);Clazz requiredType = beanWrapper.getPropertyType("name");

然后

beanWrapper.convertIfNecessary("Tom", requiredType, methodParam)

<块引用>

Spring MVC 容器如何绑定一个代表 Date 的请求参数?

如果您有需要特殊转换的人性化数据表示,您必须注册一个PropertyEditor 比如java.util.Date不知道13/09/2010是什么,所以你告诉Spring

<块引用>

Spring,使用以下 PropertyEditor 转换这个人性化的日期

binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {公共无效setAsText(字符串值){尝试 {setValue(new SimpleDateFormat("dd/MM/yyyy").parse(value));} catch(ParseException e) {设置值(空);}}公共字符串 getAsText() {return new SimpleDateFormat("dd/MM/yyyy").format((Date) getValue());}});

在调用 convertIfNecessary 方法时,Spring 会查找任何已注册的 PropertyEditor,它负责转换提交的值.要注册您的 PropertyEditor,您可以

春季 3.0

@InitBinder公共无效活页夹(WebDataBinder 活页夹){//如上图}

旧式 Spring 2.x

@Override公共无效initBinder(HttpServletRequest请求,ServletRequestDataBinder绑定器){//如上图}

For request parameters representing string, number, and boolean values, the Spring MVC container can bind them to typed properties out of the box.

How do you have the Spring MVC container bind a request parameter representing a Date?

Speaking of which, how does the Spring MVC determine the type of a given request parameter?

Thanks!

解决方案

How does the Spring MVC determine the type of a given request parameter ?

Spring makes use of ServletRequestDataBinder to bind its values. The process can be described as follows

/**
  * Bundled Mock request
  */
MockHttpServletRequest request = new MockHttpServletRequest();
request.addParameter("name", "Tom");
request.addParameter("age", "25");

/**
  * Spring create a new command object before processing the request
  *
  * By calling <COMMAND_CLASS>.class.newInstance(); 
  */
Person person = new Person();

...

/**
  * And then with a ServletRequestDataBinder, it binds the submitted values
  * 
  * It makes use of Java reflection To bind its values
  */
ServletRequestDataBinder binder = new ServletRequestDataBinder(person);
binder.bind(request);

Behind the scenes, DataBinder instances internally makes use of a BeanWrapperImpl instance which is responsible for set up the values of the command object. With getPropertyType method, it retrieves the property type

If you see the submitted request above (of course, by using a mock), Spring will call

BeanWrapperImpl beanWrapper = new BeanWrapperImpl(person);

Clazz requiredType = beanWrapper.getPropertyType("name");

And Then

beanWrapper.convertIfNecessary("Tom", requiredType, methodParam)

How does Spring MVC container bind a request parameter representing a Date ?

If you have human-friendly representation of data which needs special conversion, you must register a PropertyEditor For instance, java.util.Date does not know what 13/09/2010 is, so you tell Spring

Spring, convert this human-friendly date by using the following PropertyEditor

binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
    public void setAsText(String value) {
        try {
            setValue(new SimpleDateFormat("dd/MM/yyyy").parse(value));
        } catch(ParseException e) {
            setValue(null);
        }
    }

    public String getAsText() {
        return new SimpleDateFormat("dd/MM/yyyy").format((Date) getValue());
    }        

});

When calling convertIfNecessary method, Spring looks for any registered PropertyEditor which takes care of converting the submitted value. To register your PropertyEditor, you can either

Spring 3.0

@InitBinder
public void binder(WebDataBinder binder) {
    // as shown above
}

Old-style Spring 2.x

@Override
public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
    // as shown above
}

这篇关于Spring MVC - 绑定日期字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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