Languages
[Edit]
EN

Spring Framework - escape html code

9 points
Created by:
JustMike
3527

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. Java - how to escape html special characters? 
Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.