Languages
[Edit]
EN

Spring Framework - escape html code

9 points
Created by:
Sylvie-Justice
280

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>

See also

  1. HTML - characters that should be escaped 
  2. Java - how to escape html special characters? 
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join