如何在 freemarker 模板中转义来自网络服务的数据中的“&”?
How to escape '&' in data coming from webservice in freemarker template?
自由标记:
如果我的数据是:
x = [{
name= satyajit,
company = hewlett & packard
}, {
name= akanksha,
company = google & co
}]
以下是我想在 ftl 模板中访问上述列表的方式。
${x}
但是,上面的代码会抛出一个错误:实体名称必须紧跟在实体引用中的“&”之后。
有没有办法从 freemarker 模板中的网络服务数据中转义数据中的“&”,例如 'hewlett & packard' 中的“&”?
编辑:
遇到错误:
[Fatal Error] NewFile.xml:27:57: The entity name must immediately follow the '&' in the entity reference.
Exception in thread "main" org.xml.sax.SAXParseException; systemId: file:/D:/NewFile.xml; lineNumber: 27; columnNumber: 57; The entity name must immediately follow the '&' in the entity reference.
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
找了一段时间发现freemarker builtin html可以解决我的情况。就我而言:
${x?html}
在 <#escape x as x?html> 指令中换行文本块。
public static final String ESCAPE_PREFIX = "<#ftl strip_whitespace=true><#escape x as x?html>";
public static final String ESCAPE_SUFFIX = "</#escape>";
ESCAPE_PREFIX + templateText + ESCAPE_SUFFIX
参考:http://freemarker.org/docs/dgui_misc_autoescaping.html
http://watchitlater.com/blog/2011/10/default-html-escape-using-freemarker/
自由标记:
如果我的数据是:
x = [{
name= satyajit,
company = hewlett & packard
}, {
name= akanksha,
company = google & co
}]
以下是我想在 ftl 模板中访问上述列表的方式。
${x}
但是,上面的代码会抛出一个错误:实体名称必须紧跟在实体引用中的“&”之后。
有没有办法从 freemarker 模板中的网络服务数据中转义数据中的“&”,例如 'hewlett & packard' 中的“&”?
编辑:
遇到错误:
[Fatal Error] NewFile.xml:27:57: The entity name must immediately follow the '&' in the entity reference.
Exception in thread "main" org.xml.sax.SAXParseException; systemId: file:/D:/NewFile.xml; lineNumber: 27; columnNumber: 57; The entity name must immediately follow the '&' in the entity reference.
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
找了一段时间发现freemarker builtin html可以解决我的情况。就我而言:
${x?html}
在 <#escape x as x?html> 指令中换行文本块。
public static final String ESCAPE_PREFIX = "<#ftl strip_whitespace=true><#escape x as x?html>";
public static final String ESCAPE_SUFFIX = "</#escape>";
ESCAPE_PREFIX + templateText + ESCAPE_SUFFIX
参考:http://freemarker.org/docs/dgui_misc_autoescaping.html
http://watchitlater.com/blog/2011/10/default-html-escape-using-freemarker/