【问题标题】:Passing Data between Service and Controller Grails在服务和控制器 Grails 之间传递数据
【发布时间】:2017-04-12 16:47:36
【问题描述】:

我是 grails 新手,我不知道如何解决这个问题。我需要将数据从服务类 (someserviceclass.groovy) 中的方法传递到 Grails 中的动作控制器。我该怎么做?

【问题讨论】:

  • 控制器调用服务...或者应该这样做
  • 不,我希望服务方法异步调用控制器中的操作,将数据传递给它。
  • 这听起来不对...认为您需要在问题中解释您的用例

标签: grails service controller


【解决方案1】:

控制器

class MyController {
    def myService

    def myAction() {
        def values = myService.getData()

        [viewVarible: values]  // since our method return def we don't need to use return keyword
    }
 }

服务

class MyService {

    def getData() {
        def data = [:]

        data.put("key1", "value1")
        data.put("key2", "value2")
        data.put("key3", "value3")

        return data   // we could have just said data without the return keyword
    }
}

【讨论】:

    猜你喜欢
    • 2014-01-30
    • 2013-07-30
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-29
    • 1970-01-01
    相关资源
    最近更新 更多