2010-09-14 114 views
40

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

你如何让Spring MVC容器绑定一个表示日期的请求参数?说到这些,Spring MVC如何确定给定请求参数的类型?

谢谢!

回答

62

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

Spring利用ServletRequestDataBinder来绑定它的值。该过程可以被描述如下

/** 
    * 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实例设置命令对象的值,其负责的。随着​​方法,它检索属性类型

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

BeanWrapperImpl beanWrapper = new BeanWrapperImpl(person); 

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

而且随后

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

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

如果您有需要特殊的转换数据的人类友好的表示,你必须注册一个PropertyEditor例如,java.util.Date不知道什么13/09/2010是,这样你就告诉Spring

春天,使用下面的属性编辑

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方法,春季查找任何registere d PropertyEditor负责转换提交的值。要注册您的属性编辑器,您可以

春3.0

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

旧式春2.x的

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

非常感谢。 – 2010-09-14 05:25:51

+1

Spring 2.5.x也有一个@InitBinder:http://static.springsource.org/spring/docs/2.5.x/reference/mvc.html#mvc-ann-initbinder – Rihards 2011-03-27 00:20:13

+2

谢谢你,因为这似乎是一个全面的答案,但对于非专家来说,它确实有助于包含一个简单的实例。 – Marquez 2012-12-12 15:15:18

30

在补充亚瑟的非常完整的答案:在一个简单的日期的情况下,字段,你不必实现整个PropertyEditor。您只需使用您只需传递日期格式的CustomDateEditor

//put this in your Controller 
//(if you have a superclass for your controllers 
//and want to use the same date format throughout the app, put it there) 
@InitBinder 
private void dateBinder(WebDataBinder binder) { 
      //The date format to parse or output your dates 
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); 
      //Create a new CustomDateEditor 
    CustomDateEditor editor = new CustomDateEditor(dateFormat, true); 
      //Register it as custom editor for the Date type 
    binder.registerCustomEditor(Date.class, editor); 
} 
+0

向您的代码添加一些注释将有助于OP了解您的答案建议。检查这个[metaSO问题](http://meta.stackexchange.com/questions/7656/how-do-i-write-a-good-answer-to-a-question)和[Jon Skeet:Coding Blog]( http://msmvps.com/blogs/jon_skeet/archive/2009/02/17/answering-technical-questions-helpfully.aspx)如何给出正确的答案。 – Yaroslav 2012-12-27 10:29:39