Java Spring MVC-绑定日期字段

Java Spring MVC-绑定日期字段,java,spring,spring-mvc,Java,Spring,Spring Mvc,对于表示字符串、数字和布尔值的请求参数,SpringMVC容器可以将它们绑定到开箱即用的类型化属性 如何让SpringMVC容器绑定表示日期的请求参数 说到这里,Spring MVC如何确定给定请求参数的类型 谢谢 SpringMVC如何确定给定请求参数的类型 Spring使用绑定其值。该过程可描述如下 /** * Bundled Mock request */ MockHttpServletRequest request = new MockHttpServletRequest();

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

如何让SpringMVC容器绑定表示日期的请求参数

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

谢谢

SpringMVC如何确定给定请求参数的类型

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

/**
  * 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();
在幕后,实例在内部使用一个实例,该实例负责设置命令对象的值。方法检索属性类型

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

BeanWrapperImpl beanWrapper = new BeanWrapperImpl(person);

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

beanWrapper.convertIfNecessary("Tom", requiredType, methodParam)
SpringMVC容器如何绑定表示日期的请求参数

如果您有需要特殊转换的数据的人性化表示,那么您必须注册一个示例,java.util.Date不知道什么是2010年9月13日,所以您告诉Spring

Spring,使用以下PropertyEditor转换此人性化日期

调用ConvertifRequired方法时,Spring会查找负责转换提交值的任何已注册PropertyEditor。要注册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
}

作为对Arthur非常完整答案的补充:对于一个简单的日期字段,您不必实现整个PropertyEditor。您只需将要使用的日期格式传递给一个:

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

Spring2.5.x也有一个@InitBinder:谢谢你,因为这似乎是一个全面的答案,但对于非专家来说,包含一个简单的实用示例将非常有帮助。谢谢!非常清楚。但我想知道如何让这段代码全球化,而不是局限于一个控制器。如果您还可以使用
转换器
来实现这一点?@arthurronal非常感谢您!上帝啊在2021年,这仍然是原始的吗?在代码中添加一些注释将有助于OP理解您的答案建议。检查此项以及如何给出正确答案。
@Override
public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
    // as shown above
}
//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);
}