0

I would like to highlight a text which is returned from server like below

This is my name <em>John</em>

I debugged in the server and the above string is returned. But the text is not getting highlighted when it is rendered in the browser.

It shows up like below

"This is my name <em>John</em>"

The browser view source is shown below

This is my name &lt;em&gt;John&lt;/em&gt;

Should I escape the "<" and ">" symbols ? . I am using spring mvc in this application and thymeleaf for generating html

<div th:if="${not #lists.isEmpty(results)}">
                    <h3>Search Results</h3>
                    <table class="table table-striped">
                        <tr>
                            <th>Id</th>
                            <th>Title</th>

                        </tr>
                        <tr th:each="result : ${results}">
                            <td th:text="${result.id}">Id</td>
                            <td th:text="${result.title}">Title</td>

                        </tr>
                    </table>

                </div>
lives
  • 1,243
  • 5
  • 25
  • 61
  • You should either change your encoding on the HTML page or you should use StringEscapeUtils in Java to escape your text. – Ayush Feb 28 '17 at 07:20
  • it depends on how you are displaying in browser.if you are using jquery try using $('id').html() not text() – Vikram Singh Feb 28 '17 at 07:21

2 Answers2

1

This is the solution

Title

Instead of th.text , use th.utext

lives
  • 1,243
  • 5
  • 25
  • 61
-3

You can use HTML5 tag also for Highlight

<p>test test test <mark>test</mark> today.</p>
Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100