如果我想在控制器中的多个方法之间共享一个对象,那么创建一个实例 var 是错误的吗?例如
class MyController {
def index() {
def user = verifyUserLogin()
[messages:getMessages(user)]
}
private verifyUserLogin() {
...
return user
}
private getMessages(user) {
...do something with `user`...
return messages
}
}
会成为
class MyController {
private user
def index() {
verifyUserLogin()
[messages:getMessages()]
}
private void verifyUserLogin() {
...
this.user = user
return
}
private getMessages() {
...do something with `user`...
return messages
}
}