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

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

问题描述

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

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

你如何拥有Spring MVC容器绑定一个表示Date的请求参数?

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

说到这,Spring MVC如何确定给定请求参数的类型?

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

谢谢!

推荐答案


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

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

Spring使用 ServletRequestDataBinder 绑定其值。该过程可以描述如下

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 bind the submitted values
  * 
  * It makes use of Java reflection To bind its values
  */
ServletRequestDataBinder binder = ServletRequestDataBinder(person);
binder.bind(request);

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

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

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

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

然后

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




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

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

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

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,使用以下PropertyEditor转换此人类友好日期

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

});

当调用convertIfNecessary方法时,Spring会查找任何注册转换提交值的PropertyEditor。要注册您的PropertyEditor,您可以

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

Spring 3.0

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

旧式Spring 2.x

Old-style Spring 2.x

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

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

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