Spring框架中提供了一个用于HTML字符转义的工具类org.springframework.web.util.HtmlUtils
,使用方式如下代码:
import org.springframework.web.util.HtmlUtils; public class TestHtmlEscape { public static void main(String[] args) { String html = "" + "<!DOCTYPE html>\n" + "<html lang=\"en\">\n" + "<head>\n" + " <meta charset=\"UTF-8\">\n" + " <title>鹞之神乐</title>\n" + "</head>\n" + "<body>\n" + "\n" + "</body>\n" + "</html>"; String htmlEscape = HtmlUtils.htmlEscape(html); System.err.println(htmlEscape); } }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>鹞之神乐</title> </head> <body> </body> </html>
未经允许不得转载:鹞之神乐 » 使用Spring提供的HtmlUtils实现HTML字符转义