仅在生产环境中编码的Grails GSP表达式
我试图找出是哪个配置导致了我在GrailsV4.0.6中看到的行为 如果我有一个GSP表达式,比如仅在生产环境中编码的Grails GSP表达式,grails,gsp,Grails,Gsp,我试图找出是哪个配置导致了我在GrailsV4.0.6中看到的行为 如果我有一个GSP表达式,比如${myMessage},而myMessage包含html标记,比如hello world,当我在开发模式下本地运行时,这是预期的呈现 但是,如果部署到生产模式,Grails将对html标记进行编码,因此它不会按预期呈现。我确信这是某个地方的配置,可能与application.yml中的编码或编解码器配置有关,但我看不到在开发模式和生产模式之间有什么特别的区别 我还将注意到,我看到这一切的时候都是在
${myMessage}
,而myMessage包含html标记,比如hello world代码>,当我在开发模式下本地运行时,这是预期的呈现
但是,如果部署到生产模式,Grails将对html标记进行编码,因此它不会按预期呈现。我确信这是某个地方的配置,可能与application.yml中的编码或编解码器配置有关,但我看不到在开发模式和生产模式之间有什么特别的区别
我还将注意到,我看到这一切的时候都是在从我的自定义标记库呈现的模板中使用表达式的时候,例如:
out << render(template:"bookTemplate",model:"[book: myBook]")
out
默认情况下,Grails安全运行并转义${}中的所有内容
GSP中的表达式。所有的标准GSP标签也都是安全的
默认值,转义任何相关属性值
那么,当您想要阻止Grails逃逸一些
内容?将HTML放入数据库有一些有效的用例
并按原样呈现,只要内容是可信的。这样
在这种情况下,您可以告诉Grails内容应该是安全的
渲染为原始,即没有任何转义:
${raw(page.content)}
您在这里看到的raw()方法可从controllers、tag中获得
图书馆和普惠制网页
后来:
GSP具有自动对GSP表达式进行HTML编码的功能,
从Grails2.3开始,这是默认配置。默认值
新创建的Grails的配置(可在application.yml中找到)
应用程序如下所示:
grails:
views:
gsp:
encoding: UTF-8
htmlcodec: xml # use xml escaping instead of HTML4 escaping
codecs:
expression: html # escapes values inside ${}
scriptlets: html # escapes output from scriptlets in GSPs
taglib: none # escapes output from taglibs
staticparts: none # escapes output from static template parts
GSP提供了几个编解码器,在将页面写入
答复。编解码器在编解码器块中配置,并且
如下所述:
expression - The expression codec is used to encode any code found within ${..} expressions. The default for newly created application is html encoding.
scriptlet - Used for output from GSP scriplets (<% %>, <%= %> blocks). The default for newly created applications is html encoding
taglib - Used to encode output from GSP tag libraries. The default is none for new applications, as typically it is the responsibility of the tag author to define the encoding of a given tag and by specifying none Grails remains backwards compatible with older tag libraries.
staticparts - Used to encode the raw markup output by a GSP page. The default is none.
expression-表达式编解码器用于对${..}表达式中的任何代码进行编码。新创建的应用程序的默认值是html编码。
scriptlet-用于GSP scriplets(、块)的输出。新创建的应用程序默认为html编码
taglib-用于对GSP标记库的输出进行编码。对于新的应用程序,默认值为none,因为标记作者通常负责定义给定标记的编码,并且通过指定none,Grails将保持与旧标记库的向后兼容。
staticparts-用于对GSP页面输出的原始标记进行编码。默认值为“无”。
感谢@Michal_Szulc的回复。实际上,我已经阅读了该文档,它确实解释了为什么我需要使用raw()来解决我的问题。我想弄明白的是,为什么在开发模式下本地运行时,我不需要使用raw(),但在生产模式下我需要使用?如果有“更严格的生产”的配置,是什么?