EN
Spring Framework - escape html code
9
points
In this article, we would like to show how to escape HTML using Spring Framework.
Quick solution:
1. Create *.java
file:
import org.springframework.web.util.HtmlUtils
class Program {
public static void main(String[] args) {
String html = "<p>Some text here...</p>";
String escaped1Html = HtmlUtils.htmlEscape(html); // "ISO-8859-1" by default
String escaped2Html = HtmlUtils.htmlEscape(html, "UTF-8");
System.out.println(escaped1Html);
System.out.println(escaped2Html);
}
}
2. Attach maven dependencies to *.pom
file:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>