Spring 5 并在 Springs 自由标记宏中转义
Spring 5 and escaping in Springs freemarker macros
更新到 Spring 5 后,Spring freemarker 宏的所有输出都被转义。在 <@spring.message> 中,消息被转义,而在 <@spring.formRadioButtons> 中,分隔符属性被转义 f.x。 “
”。
配置:
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer config = new FreeMarkerConfigurer();
config.setTemplateLoaderPath("/WEB-INF/templates/ftl/");
Properties props = new Properties();
props.put("template_update_delay", getFreemarkerUpdateDelay());
props.put("template_exception_handler", getFreemarkerExceptionHandler());
props.put("url_escaping_charset", WebConstants.CHAR_SET_UTF_8);
config.setFreemarkerSettings(props);
config.setDefaultEncoding(WebConstants.CHAR_SET_UTF_8);
return config;
}
我们使用 Freemarker 2.3.28 和 Spring 5.0.7.RELEASE
关于如何关闭宏输出转义的任何想法的 TIA。
查看 https://github.com/spring-projects/spring-framework/blob/master/spring-webmvc/src/main/resources/org/springframework/web/servlet/view/freemarker/spring.ftl,它以 <#ftl output_format="HTML" ...>
开头,因此特定模板已为自身激活 HTML 自动转义,这比 outputFormat
Configuration
设置。由于 MessageSource.getMessage
只能 return String
(因此不是 TemplateMarkupOutputModel
,它不会被 FreeMarker 自动转义),在我看来,维护者已经认真对待这里的监督。他们严重破坏了向后兼容性(假设它确实没有在 Spring 4 中转义),但更糟糕的是,我不明白他们要如何支持不转义。没有 spring.message
之类的变体。 (它再次反击说 spring.message
不是一个函数,因为那样你就可以在它上面应用 ?noesc
。)所以你应该向他们报告。
更新:早些时候我建议通过 template_configurations
和 auto_escaping_policy
在 spring.ftl
上禁用自动转义,但事实证明这是错误的,因为一些宏确实在 [=30] 中转义=] 4 via ?html
,他们在切换到自动转义时将其删除。那么那些就不会逃脱了,这又是错误的...
更新到 Spring 5 后,Spring freemarker 宏的所有输出都被转义。在 <@spring.message> 中,消息被转义,而在 <@spring.formRadioButtons> 中,分隔符属性被转义 f.x。 “
”。
配置:
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer config = new FreeMarkerConfigurer();
config.setTemplateLoaderPath("/WEB-INF/templates/ftl/");
Properties props = new Properties();
props.put("template_update_delay", getFreemarkerUpdateDelay());
props.put("template_exception_handler", getFreemarkerExceptionHandler());
props.put("url_escaping_charset", WebConstants.CHAR_SET_UTF_8);
config.setFreemarkerSettings(props);
config.setDefaultEncoding(WebConstants.CHAR_SET_UTF_8);
return config;
}
我们使用 Freemarker 2.3.28 和 Spring 5.0.7.RELEASE
关于如何关闭宏输出转义的任何想法的 TIA。
查看 https://github.com/spring-projects/spring-framework/blob/master/spring-webmvc/src/main/resources/org/springframework/web/servlet/view/freemarker/spring.ftl,它以 <#ftl output_format="HTML" ...>
开头,因此特定模板已为自身激活 HTML 自动转义,这比 outputFormat
Configuration
设置。由于 MessageSource.getMessage
只能 return String
(因此不是 TemplateMarkupOutputModel
,它不会被 FreeMarker 自动转义),在我看来,维护者已经认真对待这里的监督。他们严重破坏了向后兼容性(假设它确实没有在 Spring 4 中转义),但更糟糕的是,我不明白他们要如何支持不转义。没有 spring.message
之类的变体。 (它再次反击说 spring.message
不是一个函数,因为那样你就可以在它上面应用 ?noesc
。)所以你应该向他们报告。
更新:早些时候我建议通过 template_configurations
和 auto_escaping_policy
在 spring.ftl
上禁用自动转义,但事实证明这是错误的,因为一些宏确实在 [=30] 中转义=] 4 via ?html
,他们在切换到自动转义时将其删除。那么那些就不会逃脱了,这又是错误的...