我正在将现有的Grails 2.5.6项目转换为Grails 3.3.11。
对于我现有的应用程序(Grails 2.5.6),插件描述符的代码如下所示:
def doWithApplicationContext = { applicationContext ->
def config = applicationContext.grailsApplication.config
def key = config.property.key
key.put(Constants.RESULT_CONST, [controller: "results", action: "showData", templatePath: "/results/data"])
}此代码适用于较早版本的grails。但是在升级到grails3.3.11之后,它会抛出异常:java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
这是在排队:
key.put(Constants.RESULT_CONST, [controller: "results", action: "showData", templatePath: "/results/data"])
在查看键的类型(即config.property.key )之后,它将类型显示为org.grails.config.NavigableMap$NullSafeNavigator。
旧版本的是LinkedHashMap。property.key设置在/grails/conf/applic.groovy property.key = [:]下面的application.groovy上
我还尝试将插件描述符中的property.key类型设置为java.util.HashMap。但它似乎没有采用新的类型。
我在这里做错了什么?
发布于 2020-03-27 14:09:58
而不是尝试使用以下方法动态地执行此操作:
def doWithApplicationContext = { applicationContext ->
def config = applicationContext.grailsApplication.config
def key = config.property.key
key.put(2, [controller: "results", action: "showData", templatePath: "/results/data"])
}您可以像这样在grails-app/conf/plugin.yml中定义这些值:
---
property:
key:
'2':
controller: results
action: showData
templatePath: '/results/data`编辑
问题已经变了,上面的问题已经不再有效了。
而不是这样做:
def config = applicationContext.grailsApplication.config
def key = config.property.key
key.put(Constants.RESULT_CONST, [controller: "results", action: "showData", templatePath: "/results/data"])您可以将其简化为:
config.merge([property: [key: [Constants.RESULT_CONST, [controller: "results", action: "showData", templatePath: "/results/data"]]]])发布于 2020-04-14 05:52:25
谢谢杰夫的意见和建议。
下面是设置配置参数并在控制器或任何其他grails组件中检索它们的统一代码。
插件描述符:
class ResultGrailsPlugin extends Plugin {
void doWithApplicationContext() {
config.merge(['property': ['key': ["${Constants.RESULT_CONST}": [controller: "results", action: "showData", templatePath: "/results/data"]]]])
}
}控制器:
class ResultController {
def index() {
def resultConfigMap = grailsApplication.config.get('property.key.' + Constants.RESULT_CONST)
...
}
}https://stackoverflow.com/questions/60887352
复制相似问题