[英]Escape HTML in Languages with Accented Letters
Which html escape method in Java is recommended to use if I don't want it to escape accented characters, for example, in string "Matías", accented í should remain unescaped.如果我不想转义重音字符,建议使用 Java 中的哪种 html 转义方法,例如,在字符串“Matías”中,重音 í 应保持未转义。
Both StringEscapeUtils.escapeHtml() and Springs's HtmlUtils.htmlEscape() escape these letters. StringEscapeUtils.escapeHtml() 和 Springs 的 HtmlUtils.htmlEscape() 都对这些字母进行了转义。
Using Spring's htmlEscape(String input, String encoding)
you can pass an encoding like "UTF-8" .使用 Spring 的
htmlEscape(String input, String encoding)
您可以传递像"UTF-8"这样的编码。 According to the JavaDoc the characters won't be escaped if they're in the given encoding (or at least that's how I understand it).根据 JavaDoc,如果字符处于给定的编码中(或者至少我是这样理解的),它们将不会被转义。
Guava 的 HtmlEscapers.htmlEscaper().escape(inputString) 在没有指定编码的情况下做到了这一点
Using Apache commons-text:使用 Apache 公共文本:
public static final CharSequenceTranslator ESCAPE_CUSTOM =
new AggregateTranslator(
new LookupTranslator(EntityArrays.BASIC_ESCAPE),
new LookupTranslator(EntityArrays.HTML40_EXTENDED_ESCAPE)
);
ESCAPE_CUSTOM.translate(input);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.