grails - Grails Controller 操作中的多个域类绑定(bind)

标签 grails data-binding

使用 grails 2.3.7。我正在尝试使用 Grails Controller 操作参数绑定(bind)。如果我有这个代码:

class TestController {
    def test(MyClass1 myClass1) {
        log.debug(myClass1)
    }
}

使用 http://locahost:8080/myapp/test/test/1 从 DB 中正确获取 myClass1 .
但现在我想通过两个域类。我试过这段代码:
class TestController {
    def test(@RequestParameter('obj1') MyClass1 myClass1, 
             @RequestParameter('obj2') MyClass2 myclass2) {

        log.debug(myClass1)
        log.debug(myClass2)
    }
}

并使用 http://localhost:8080/myapp/test/test?obj1.id=1&obj2.id=3 访问, 什么都没有提取。这是在 Controller 操作中使用数据绑定(bind)的正确方法吗?或者这是不可能的?

提前致以问候和感谢。

最佳答案

您可以在 Controller 中使用它和其中之一:

// binds request parameters to a target object
    bindData(target, params)
    // exclude firstName and lastName
    bindData(target, params, [exclude: ['firstName', 'lastName']])

    // only use parameters starting with "author." e.g. author.email
    bindData(target, params, "author")
    bindData(target, params, [exclude: ['firstName', 'lastName']], "author")

    // using inclusive map
    bindData(target, params, [include: ['firstName', 'lastName']], "author")

def User bindUser(params)  {
        def User user = new User()
        def Human human = new Human() 

           bindData(user, params["user"])
           bindData(human, params["humna"])
         if(!human) 
         human.save(failOnError:true) 
         if(!user)
         user.save(failOnError:true)
        }
         //alloha~
    } 

关于grails - Grails Controller 操作中的多个域类绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22712916/

相关文章:

grails - 生成的PersonController期望授权包含单词ROLE

ajax - 为什么我的grails复选框onclick远程功能ajax调用显示更新错误?

grails - Grails 集成测试中的一对多关联数据绑定(bind)问题

c# - 如何将 Combobox 绑定(bind)到 ReactiveUI 中的命令?

silverlight - Silverlight 的 BindingBase.Delay

Grails:如何根据格式(JSON、HTML)提供对错误 500 的不同响应?

grails - 控制 Camel 路线的启动和关闭

javascript - 围绕按钮链接到链接的按钮

wpf - 数据绑定(bind)如何避免 WPF 中的递归更新?

.net - 什么时候应该谨慎使用 .NET 中的数据绑定(bind)?