在grails中获取域上的特定实例

在grails中获取域上的特定实例,grails,Grails,我有一个gsp文件,它调用如下方法: <g:link id="${child.id}" action="callChildProfile" controller="profile">${child.firstname}</g:link> def index() { def messages = currentUserTimeline() [profileMessages: messages] System

我有一个gsp文件,它调用如下方法:

<g:link id="${child.id}" action="callChildProfile" controller="profile">${child.firstname}</g:link>
        def index() {
        def messages = currentUserTimeline()
        [profileMessages: messages]
         System.out.println(childInstance + " child here")
        [childInstance : childInstance]
    } 
此方法将子实例设置为名为child instance的公共变量,但当发生重定向时,该变量将重置。 我重定向的原因是我想从这个控制器加载索引页

索引如下所示:

<g:link id="${child.id}" action="callChildProfile" controller="profile">${child.firstname}</g:link>
        def index() {
        def messages = currentUserTimeline()
        [profileMessages: messages]
         System.out.println(childInstance + " child here")
        [childInstance : childInstance]
    } 

控制器方法(操作)中的变量具有局部作用域,因此,只能在该方法中使用。您应该从新实例传递id,并使用该id检索对象

redirect action: "index", id: childInstance.id
索引可以是

def index(Long id){
    childInstance = Child.get(id)
然后您可以得出结论,您不需要callChildProfile方法

或者您可以使用params

def index(){
    childInstance = Child.get(params.id)
    if(childInstance){
        doSomething()
    }
    else{
        createOrGetOrDoSomethingElse()
    }
}

控制器方法(操作)中的变量具有局部作用域,因此,只能在该方法中使用。您应该从新实例传递id,并使用该id检索对象

redirect action: "index", id: childInstance.id
索引可以是

def index(Long id){
    childInstance = Child.get(id)
然后您可以得出结论,您不需要callChildProfile方法

或者您可以使用params

def index(){
    childInstance = Child.get(params.id)
    if(childInstance){
        doSomething()
    }
    else{
        createOrGetOrDoSomethingElse()
    }
}

默认情况下,控制器是原型范围的,这意味着调用
callChildProfile
的请求和调用
index
的请求之间使用的
ProfileController
实例将不同。因此,对象级别
childInstance
变量在请求之间不可用

要在
索引
调用中使用
子实例
,请查看以下方法:


当从
index
返回
Map
时,链调用的模型将自动添加,因此
callChildProfile
中的
childInstance
将可用于gsp。

默认情况下,控制器是原型范围,这意味着调用
callChildProfile
的请求与调用
index
的请求之间使用的
ProfileController
实例不同。因此,对象级别
childInstance
变量在请求之间不可用

要在
索引
调用中使用
子实例
,请查看以下方法:


当从
index
返回
Map
时,将自动添加链调用的模型,因此,您的
callChildProfile
中的
childInstance
将可用于gsp。

但这是否意味着每次我调用index时都必须给它一个长id?我在回答中写了一个备选方案,但这是否意味着每次调用index时都必须给它一个长id?我在回答中写了一个备选方案