有没有逃脱推荐的方式<,>,"和&字符时输出HTML中普通的Java代码?(也就是说,除了手动执行以下操作之外)。
<,>
"
&
HTML
Java
String source = "The less than sign (<) and ampersand (&) must be escaped before using them in HTML"; String escaped = source.replace("<", "<").replace("&", "&"); // ...
来自Apache Commons Lang的StringEscapeUtils:
Apache Commons Lang
StringEscapeUtils
import static org.apache.commons.lang.StringEscapeUtils.escapeHtml; // ... String source = "The less than sign (<) and ampersand (&) must be escaped before using them in HTML"; String escaped = escapeHtml(source);
对于版本3:
import static org.apache.commons.lang3.StringEscapeUtils.escapeHtml4; // ... String escaped = escapeHtml4(source);