gpt4 book ai didi

java - Freemarker 中的默认转义

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:53:57 24 4
gpt4 key购买 nike

在 Freemarker 模板中,我们可以使用转义指令自动将转义应用于包含 block 内的所有插值:

<#escape x as x?html>
<#-- name is escaped as html -->
Hallo, ${name}
</#escape>

有没有办法以编程方式实现类似的效果,定义应用于模板中所有插值的默认转义,包括转义指令之外的那些?

谢谢。

最佳答案

详细说明 Attila 的回答:您可以使用类似 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>/[#ftl]。

关于java - Freemarker 中的默认转义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1265488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com