Based on @Wim Deblauwe comment on the original post, I did the following test and it worked!
Initially thought meta tag should be added just inside tag of the main file, but that’s not the case.
It can be added in the fragment as well such as the code below.
And that is what makes the fragment enconding work.
<div th_fragment="testfragment">
<meta charset="UTF-8">
coração
</div>
I use charset UTF-8. It works fine with a code like the one below, correctly displaying the “çã” characters of the word “coração”.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form>
coração
</form>
</body>
</html>
But if I move that code into a thymeleaf fragment such as below, the encoding stops working and strange characters are displayed.
testfragment.html
<div th_fragment="testfragment">
coração
</div>
test.html (inserts the code from testfragment.html)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form>
<div th_replace="testfragment.html :: testfragment"></div>
</form>
</body>
</html>
How to make charset work when using thymeleaf fragments?
It depends on how you have integrated Thymeleaf into your project.
@Wim Deblauwe Based on your comment I decided to do a test that I hadn’t done before. That is to add
Thank you @WimDeblauwe – I learn a lot from your posts and comments.
I agree andrewjames. Have learned from @Wim Deblauwe before also and would like to extend my appreciation as well.