I have to display below text in thymeleaf, but it fails to display and not sure the reason and the resolution for this.
Below is the text which is coming from JSON format and I am parsing this json object and setting it in POJO Entities for Thymeleaf to render.
I am expecting below output:
If I use <span th:text="${comment.commentText}"></span>
, email address displays properly but I lost formatting, meaning everything displays in one line regardless of line breaks, but if I use <span th:utext="${comment.commentText}"></span>
, thymeleaf fails to render (without email address field in my commentsText, thymeleaf renders text with line breaks by using th:utext
).
UPDATE:
I tried andrewJames suggested solution by replacing < and > with <
and >
It works to replace email brackets but failed to display the line breaks in thymeleaf.
Java Code:
commentData.setCommentText(comment.replaceAll("(\r\n|\r|\n|\n\r)", "<br>")
.replaceAll("\t", " ").replaceAll("<", "<").replaceAll(">", ">")
);
After replace, here is my comment.getCommentText()
data
Entered comments: <br><br>One <br>Two <br><br>My Email: <test@g.com>
Thymeleaf code
<span th:utext="${commentText}"></span>
and the output is:
I am not sure the reason why <br>
tags were not rendering if I put extra lines of code at the end of my string variable replaceAll("<", "<").replaceAll(">", ">")
Thank you for your time and any inputs would be appreciated.