【问题标题】:Set to NULL a Bound Form Field if Empty如果为空,则将绑定表单字段设置为 NULL
【发布时间】:2016-02-07 21:11:11
【问题描述】:

在 Spring MVC 中,是否可以将绑定的表单字段设置为 NULL(而不是空字符串)?

如果未输入任何内容,我的域对象字段将变为空白。在这种情况下,我需要将其设置为 NULL。

<form:input path="myObject.department" />

【问题讨论】:

标签: spring spring-mvc


【解决方案1】:

为您的字符串添加自定义编辑器,将空字符串转换为 Null

@InitBinder
public void initBinder(WebDataBinder binder) {  
    binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));  
}

【讨论】:

  • 谢谢;虽然这可行,但我们有嵌套的子元素,并且只有“叶子”元素被设置为 NULL。创建父对象。在我们的例子中,如果所有子节点都为 NULL,则父节点也应该为 NULL。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多