在我的应用程序中,许多类都有公共字段“公司”。当应用程序保存该对象时,它们必须填写公司(对此进行了验证)。公司也开会。现在,当我想使用域类作为命令对象时,公司必须已填写,否则我会收到验证错误。有什么方法可以在进行任何验证之前始终填写公司字段,这样我就不必每次都手动执行此操作。
(我尝试了自定义数据绑定器,但当请求中没有参数时它不起作用)
您可以在保存、更新或验证对象之前使用以下命令设置该属性GORM 活动 http://grails.org/doc/latest/guide/GORM.html#eventsAutoTimestamping beforeInsert
, beforeUpdate
or beforeValidate
.
在您的域中,您需要类似的东西:
import org.springframework.web.context.request.RequestContextHolder
class Foo {
String company
...
def beforeInsert = {
try {
// add some more error checking (i.e. if there is is no request)
def session = RequestContextHolder.currentRequestAttributes().getSession()
if(session) {
this.company = session.company
}
} catch(Exception e) {
log.error e
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)