我有一个Grails应用程序。我想在src / groovy / MyClass.groovy中的类中使用Grails Controller 类的值(例如MyController)
如何将Grails Controller 类中的值传递给此类?我找不到任何相关的内容。
我尝试了这个:
class MyController {
def name = "myapp"
}
Class MyClass{
def username = MyController.name
}
请纠正我。谢谢
最佳答案
在不知道自己在做什么的情况下很难确定,但是您可能想将值作为参数传递给MyClass中的方法,并且您可能不希望该值成为 Controller 类中的字段。
class MyController {
def someControllerAction() {
def name = // I don't know where you are
// getting this value, but you got it from somewhere
def mc = new MyClass()
mc.someMethod(name)
// ...
}
}
class MyClass {
def someMethod(String name) {
// do whatever you want to do with the name
}
}
或者,您可以将值作为构造函数参数传递:
class MyController {
def someControllerAction() {
def name = // I don't know where you are
// getting this value, but you got it from somewhere
def mc = new MyClass(name: name)
// ...
}
}
class MyClass {
def name
}
希望对您有所帮助。
关于grails - 将值从Grails Controller 传递到src/groovy中的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24457517/