b2238488
b2238488

Reputation: 1020

How to escape single quote (') in Thymeleaf

<h1 th:text="${'What\'s up?'}"></h1>

I want this to output

<h1>What's up?</h1>

But I get an TemplateInputException. I have tried with HTML entity but it fails the same.

Upvotes: 26

Views: 24123

Answers (5)

Vijay
Vijay

Reputation: 1

To display Oct'24

th:text="${#temporals.format(billSummary.billYearMonth,'MMM''yy')}"

Upvotes: 0

Yuxing Xie
Yuxing Xie

Reputation: 51

if you want transfer code such as onclick="showName('John')" to thymeleaf expression, use th:onclick=(${'showName'(\''+name+'\''}) that will get error:EL1065E: Unexpected escape character. And then,use double sigle quote to fix:

is this right?
<div th:onclick="${'showName('''+name+''''}"></div>

do test,WTF! double single quote is escaped to '&#39';。after some research,i found the corret way is:

correct now:
<div th:onclick="'showName(\''+${name}+'\''"></div>

you should keep ${name} as whole,as an atomic,don't use '+' operation in it.

i think only th:onclick just like that,th:text can use add operation in it.

My English is funny,but i think you can understand.

Upvotes: 0

RenRen
RenRen

Reputation: 11367

To escape a single quote you just escape it with a \'

<p th:text="'What\'s up?'"></p>
<p th:text="${myVar} + 'What\'s up?'"></p>

Upvotes: 10

Metroids
Metroids

Reputation: 20477

Double single quote. Like this:

<h1 th:text="${'What''s up?'}" />

Upvotes: 43

abraham63
abraham63

Reputation: 443

maybe use htm ascii code : &#39;

<h1 th:text="${'What&#39;s up&#63;'}"></h1>

Upvotes: 3

Related Questions