model-view-controller - Groovy/Grails中的MVC

标签 model-view-controller grails groovy

我是新手。
我们如何从Grails的 Controller 中调用域方法。

Controller :

def results = User.fetch_results

用户域:
def fetch_results {
    def users = this.get(1)
}

抱歉,如果上面的代码错误,我需要知道如何从 Controller 访问域方法。

谢谢。

我的代码是这样的

UserController:
def results = User.addUser 

用户域:
def addUser {
    def user = new User()
    user.id = 1
    user.publication_name = pub_name
    user.publication_desc =  ""
    user.edit_date = new Date()
    user.save()
    }
}

我尝试了上面的代码,但出现错误。我们如何从 Controller 中调用“addUser”方法?

谢谢。

最佳答案

您需要使该方法成为静态方法。

class User {

    def static addUser() {
        def user = new User()
        ...
        user.save()
    }
}

然后确保将User对象导入 Controller 中。

关于model-view-controller - Groovy/Grails中的MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2162731/

相关文章:

validation - 如何在Grails中添加文件类型的验证

c# - Serenity MVC 框架使用 Web 服务

model-view-controller - MVC/MVP 中的 Controller 间通信

java - Spring MVC 测试框架因 HTTP 响应 406 而失败

ruby-on-rails - has_many :through or has_and_belongs_to_many or something else?

ajax - Grails:每次 ajax 调用时发送缓存头的最佳方式

javascript - 如何从 GSP 文件引用 Javascript 文件?

java - 如何获取Hibernate Query的ResultSet中的浮点值?

RESTful Grails : how do I include related entities in my XML?

grails - Aspose:设置页面标题时看不到&符之后的文本