0

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?

jkfe
  • 549
  • 7
  • 29
  • It depends on how you have integrated Thymeleaf into your project. – andrewJames Jan 05 '21 at 23:52
  • If it's a standalone Thymeleaf integration, you can set the encoding of the template resolver: `templateResolver.setCharacterEncoding("UTF-8");`. For Spring, I think there are various SO answers already - here is [one](https://stackoverflow.com/questions/36397203/utf8-charset-with-thymeleaf). There may be better Spring approaches - such as via the main Spring config file. Finally, if you only want to deal with the text hard-coded directly in one specific HTML template, you can use `coração` instead of `coração` - (HTML escaping) but this is not exactly readable or maintainable. – andrewJames Jan 05 '21 at 23:52
  • It's a spring with thymeleaf project. I tried thymeleafViewResolver.setCharacterEncoding("UTF-8") and springResourceTemplateResolver.setCharacterEncoding("UTF-8"); like in the link you provided but did not work.I don't have templateResolver and ThymeleafViewResolver: – jkfe Jan 06 '21 at 01:55
  • Understood. For Spring: What about setting [`spring.thymeleaf.encoding`](https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#spring.thymeleaf.encoding) in the main Spring `application.properties` file? The doc does suggest that the default is already `UTF-8`, however. – andrewJames Jan 06 '21 at 02:20
  • how to implement spring.thymeleaf.encoding? Couldn't find a good reference for that. – jkfe Jan 06 '21 at 03:08
  • tried spring.thymeleaf.encoding=UTF-8 in application.properties. No difference. – jkfe Jan 06 '21 at 03:14
  • 2
    Are you sure the `testfragment.html` file is saved in `UTF-8` encoding? – Wim Deblauwe Jan 06 '21 at 09:12
  • @Wim Deblauwe No, I'm not sure. Maybe that's the problem. All that it has is what I pasted above. That is, the div with the content inside. But since it's a fragment, don't think `` can be added in it. Anything I should do to save it in utf-8 encoding? – jkfe Jan 06 '21 at 11:54
  • 1
    @Wim Deblauwe Based on your comment I decided to do a test that I hadn't done before. That is to add `` inside the div of the fragment file. That worked! I thought that tag should be used just inside the head tag. And since head tag was available just on the main file, my understanding was that it couldn't be added in the fragment file. But that's not the case. It can be added there and it works! So, issue resolved. Thank you guys. – jkfe Jan 06 '21 at 12:11
  • 1
    Thank you @WimDeblauwe - I learn a lot from your posts and comments. – andrewJames Jan 06 '21 at 14:30
  • @jkfe Maybe add the final solution as an actual answer to the question so it can be marked as answered? – Wim Deblauwe Jan 06 '21 at 14:39
  • 1
    I agree andrewjames. Have learned from @Wim Deblauwe before also and would like to extend my appreciation as well. – jkfe Jan 06 '21 at 15:08
  • @Wim Deblauwe I added the answer below. – jkfe Jan 06 '21 at 15:08

1 Answers1

2

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>
jkfe
  • 549
  • 7
  • 29