简体   繁体   中英

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.

Both StringEscapeUtils.escapeHtml() and Springs's HtmlUtils.htmlEscape() escape these letters.

Using Spring's htmlEscape(String input, String encoding) you can pass an encoding like "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).

Guava 的 HtmlEscapers.htmlEscaper().escape(inputString) 在没有指定编码的情况下做到了这一点

Using Apache commons-text:

   public static final CharSequenceTranslator ESCAPE_CUSTOM =
         new AggregateTranslator(
                 new LookupTranslator(EntityArrays.BASIC_ESCAPE),
                 new LookupTranslator(EntityArrays.HTML40_EXTENDED_ESCAPE)
         );
   ESCAPE_CUSTOM.translate(input);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM