问题描述
在最新的grails 4.0.4应用程序中,
的设置grails:
views:
gsp:
codecs:
expression: none
在Tomcat 8.5.39中作为war文件部署时似乎被忽略。 (JVM 11.0.7 + 10-post-Ubuntu-2ubuntu218.04)
添加此
<head>
...
<script>
var foo = ${[a:23,b:42,c:666] as grails.converters.JSON};
</script>
</head>
生成的grails-app/views/index.gsp
显示为
var foo = {"a":23,"b":42,"c":666};
在运行grails run-app
甚至是grails prod run-app
(!)时,但编码为
var foo = {"a":23,"b":42,"c":666};
已打包(grails prod war
)部署的war文件中。
build.gradle
未被修改,只是进行了更改
compile "org.grails.plugins:cache"
对此
compile("org.grails.plugins:cache") {
exclude module:'groovy-all'
}
这是错误还是我使用的编解码器设置错误?也许有一个插件可以覆盖此设置(如此处https://github.com/grails/grails-core/issues/10722),但我找不到任何其他yml文件。任何帮助表示赞赏!
解决方法
在Grails 4.0.3中它就像一个魅力。似乎在Grails 4.0.4中已损坏...
也许问题出在the new Groovy Page Compiler Task上。永远不会使用配置文件(又名application.yml
)变量here。但这只是在经过快速调查后,很少喝咖啡的假设;-)
解决方法或我惯用的方式(仍可在Grails 4.0.4中使用)
像这样的一些助手TagLib:
import grails.converters.JSON
class FooTagLib {
static defaultEncodeAs = [taglib:'none']
static namespace = "foo"
def json = { attrs,body ->
out << raw(attrs.data as JSON)
}
}
用法:
var foo = <foo:json data="[a:23,b:42,c:666]"/>