【问题标题】:Domain object string field auto trimming域对象字符串字段自动修剪
【发布时间】:2014-02-17 19:56:41
【问题描述】:

从 Grails 2.2.3 更新到 Grails 2.3.5(groovy 2.0.8->2.1.9)后,我发现了奇怪的行为 域对象:

class Element {
  String name
  String title
  static constraints = {
    title nullable: true
  }
}

在创建时字符串字段自动修剪,空字符串替换为null

def e = new Element(name:'', title:'  sgg gg ')
assert e.name==null
assert e.title=='sgg gg'

我在 Grails & groovy 的更新日志中找不到这个超级功能。如何禁用它?

【问题讨论】:

    标签: grails grails-orm


    【解决方案1】:

    发件人:http://grails.1312388.n4.nabble.com/Grails-2-3-Data-Binding-String-Trimming-And-Null-Conversions-td4645255.html

    Grails 2.3 中的默认行为是在数据绑定期间修剪字符串。除此之外,另一个默认行为是在数据绑定期间将空字符串(其中没有任何内容,甚至没有空格的字符串)转换为 null。这两件事按此顺序发生,因此如果您绑定一个除了空格之外什么都没有的字符串,默认行为是绑定 null,因为字符串将被修剪,然后因为它是空的,所以它将被转换为 null。这是一个明智的默认设置。有单独的配置属性可以禁用这些行为。

       // grails-app/conf/Config.groovy
       grails.databinding.convertEmptyStringsToNull=false
       grails.databinding.trimStrings=false
    

    我相信有人提到过here in the documentation

    【讨论】:

      猜你喜欢
      • 2021-09-28
      • 2011-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多