data-binding - Grails,使用 session 中的值注入(inject)/填充域对象

标签 data-binding grails

在我的应用程序中,许多类都有公共(public)字段“公司”。当应用程序保存该对象时,它们必须填写公司(对此进行了验证)。公司也开会。现在,当我想使用域类作为命令对象时,公司必须已填写,否则我会收到验证错误。有什么方法可以在进行任何验证之前始终填写公司字段,这样我就不必每次都手动执行此操作。 (我尝试了自定义数据绑定(bind)器,但是当请求中没有参数时它不起作用)

最佳答案

您可以在保存、更新或验证对象之前使用GORM events设置该属性。 beforeInsertbeforeUpdatebeforeValidate

在您的域中,您需要类似的东西:

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
        }
    }
}

关于data-binding - Grails,使用 session 中的值注入(inject)/填充域对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10829033/

相关文章:

jquery - 从 Controller 修改gsp中的表

vb.net - 如何选择绑定(bind)到 DataTable 的 Datagridview 中的可见列

asp.net - DataBind 上默认为 Null 值

c# - 在选中列表框中获取具有值的索引

Grails 3.1 无法添加库

grails - Grails GSP按钮调节

hibernate - 如何从Grails的数据库 View 创建域

spring - 将 bean map 注入(inject) Grails 服务

WinForms多线程数据绑定(bind)场景,最佳实践?

c# - 具有任意列数的可绑定(bind)数据源