如何声明Closure用作函数参数

时间:2017-03-07 11:09:21

标签: grails groovy closures

我需要根据需要附加新的URL映射,appender方法需要映射为Closure(而不是UrlMapping对象)。如果手动声明Closure将会显示的内容:

static mappings = {
    name logout: "/auth/logout"{
        action="signOut"
        controller="auth"
    }
}

所以我声明了Closure(我不知道它是否是正确的语法):

String name = "logout", url = "/auth/logout",
    action = "signOut", controller = "auth"

Closure mapping = { name "${name}" : "${url}" {
        action="${action}"
        controller="${controller}"
    }
}

并将其用于appender方法:

Holders.grailsApplication.mainContext
    .getBean("grailsUrlMappingsHolder").addMappings(mapping)

但它会引发以下错误:

  

错误context.GrailsContextLoaderListener - 初始化应用程序时出错:   没有方法签名:java.lang.String.call()适用于参数类型:    (java.util.LinkedHashMap中)   值:[hapumTransactionAppointmentvacancyTagcandidate:/ hapum / transactionappointmentvacancytagcandidate]]

     

可能的解决方案:wait(),any(),wait(long),any(groovy.lang.Closure),take(int),each(groovy.lang.Closure)

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我不确定你是否理解正确,但似乎你需要这样的东西:

String name = "logout", action = "signOut", controller = "auth"
Closure mapping = { "/${name}"(controller: controller, action: action) }