SpringMVC中 页面的String映射到接口的Date字段

目前SpringMVC中,request请求到controller中主要接受几种请求类型:
form
json
etc.

@InitBinder
public void initBinder(WebDataBinder binder){
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");  
    CustomDateEditor dateEditor = new CustomDateEditor(df, true);  
    //表示如果命令对象有Date类型的属性,将使用该属性编辑器进行类型转换  
    binder.registerCustomEditor(Date.class, dateEditor);  
}

在Controller上注入InitBinder,进行数据绑定

@ InitBinder

org.springframework.web.bind.annotation.InitBinder

public @interface InitBinder

Annotation that identifies methods which initialize the WebDataBinder which will be used for populating command and form object arguments of annotated handler methods.

由这个注释标识的方法,它初始化WebDataBinder,这个WebDataBinder用于填充被@InitBinder注释的处理方法的command和form对象。

Such init-binder methods support all arguments that RequestMapping supports, except for command/form objects and corresponding validation result objects. Init-binder methods must not have a return value; they are usually declared as void.

这个@InitBinder方法支持RequestMapping支持的所有参数,command/form对象和相关校验结果。@InitBinder方法不能有返回值,它必须声明为void。

Typical arguments are WebDataBinder in combination with WebRequest or Locale, allowing to register context-specific editors.

通常参数是WebDataBinder(由WebRequest和Locale结合而成)。

方法

String[ ] value

The names of command/form attributes and/or request parameters that this init-binder method is supposed to apply to.

@InitBinder方法需要的Command/form的属性名称或请求的参数名称。

Default is to apply to all command/form attributes and all request parameters processed by the annotated handler class. Specifying model attribute names or request parameter names here restricts the init-binder method to those specific attributes/parameters, with different init-binder methods typically applying to different groups of attributes or parameters.

默认是所有的Command/form的属性名称或请求的参数名称。指定特定的名称是为了区别不同的参数

上一篇
下一篇