如果命令值为空,如何设置默认值
How to set default value if Command value is null
我们正在使用 Grails 2.4.3,在我们的命令中
@Validateable
class FreeTextSearchCommand {
String freeText="*"
int pageSize=1
int pageIndex=1
}
并在控制器中
def freeTextSearch(FreeTextSearchCommand freeTextSearchCommand){}
因此,如果来自客户站点的值为 null,那么我希望设置默认值,但它不起作用
So if value comes from Client site is null then I want my default
value set , but it does not work
如果将 null 绑定到 属性,则结果应该为 null。这就是数据绑定器所做的,也是绑定器应该做的。如果你想强加一些自定义规则,你可以通过多种方式来实现。一种是使用 BindUsing
注释。
@Validateable
class FreeTextSearchCommand {
@BindUsing({obj, source ->
// bind '*' if there is no corresponding request parameter value...
source['freetext'] ?: '*'
})
String freeText
// etc...
int pageSize=1
int pageIndex=1
}
有关详细信息,请参阅 http://grails.github.io/grails-doc/2.4.3/guide/theWebLayer.html#dataBinding。
希望对您有所帮助。
我们正在使用 Grails 2.4.3,在我们的命令中
@Validateable
class FreeTextSearchCommand {
String freeText="*"
int pageSize=1
int pageIndex=1
}
并在控制器中
def freeTextSearch(FreeTextSearchCommand freeTextSearchCommand){}
因此,如果来自客户站点的值为 null,那么我希望设置默认值,但它不起作用
So if value comes from Client site is null then I want my default value set , but it does not work
如果将 null 绑定到 属性,则结果应该为 null。这就是数据绑定器所做的,也是绑定器应该做的。如果你想强加一些自定义规则,你可以通过多种方式来实现。一种是使用 BindUsing
注释。
@Validateable
class FreeTextSearchCommand {
@BindUsing({obj, source ->
// bind '*' if there is no corresponding request parameter value...
source['freetext'] ?: '*'
})
String freeText
// etc...
int pageSize=1
int pageIndex=1
}
有关详细信息,请参阅 http://grails.github.io/grails-doc/2.4.3/guide/theWebLayer.html#dataBinding。
希望对您有所帮助。