在 Freemarker 模板中,我们可以使用 escape 指令自动将转义应用于包含的块内的所有插值:
<#escape x as x?html>
<#-- name is escaped as html -->
Hallo, ${name}
</#escape>
有没有一种方法可以以编程方式实现类似的效果,定义应用于模板中所有插值的默认转义,包括那些外部转义指令?
Thanks.
详细说明阿提拉的答案:您可以使用类似的类this one然后像这样包装你的模板加载器:
final TemplateLoader templateLoader = new ClassTemplateLoader(this.getClass(), templatePath) {
/**
* Replaces the normal template reader with something that changes the default
* escaping to HTML as to avoid XSS attacks.
*/
@Override
public Reader getReader(Object templateSource, String encoding) throws IOException {
return new WrappingReader(super.getReader(templateSource, encoding), "<#escape x as x?html>", "</#escape>");
}
};
如果您在添加的部分中不包含换行符,则不会出现行编号问题。但是,您不能将 /[#ftl] 与此方法一起使用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)