• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Inserting raw HTML into Thymeleaf template  RSS feed

 
Master Rancher
Posts: 1162
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:... If this shows up in the web page as the literal text " <br />", then it's likely because you're trying to inject it into a template that is being processed by a template engine, which will escape anything that looks like HTML. Depending on the template language, there are different ways of injecting literal HTML.


That could be, I'm not sure anymore, but I think I was using thymeleaf
 
Saloon Keeper
Posts: 9856
199
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to inject raw HTML into a Thymeleaf template, you need to use th:utext instead of th:text. However, this is very questionable practice, and an indication that the application design is off. Not only that, but if the HTML that you want to inject is based on user input, it can lead to security issues.
 
Daniel Demesmaecker
Master Rancher
Posts: 1162
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was returning a warning message from my controller when the user did something wrong while registering, it was not so mutch full html I wanted to return, just seperate the lines in case of multiple errors...
 
Stephan van Hulst
Saloon Keeper
Posts: 9856
199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a styling issue, not a content issue. Inserting line breaks just to space out text in some way is not the right approach. You should send your error messages to the template as a list, and then let the template insert the correct HTML for each separate error message:
 
Daniel Demesmaecker
Master Rancher
Posts: 1162
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah but that would mean I need to iterate the list in a scriptlet, I've always bin told to avoid scriptlets as mutch as possible
 
Stephan van Hulst
Saloon Keeper
Posts: 9856
199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean business logic in a template? Yes, that's bad. Don't do business logic in a template. A loop over a view model that you passed to the template engine? That's different. That's not business logic. That's just using the template engine the way it was intended.

You pre-calculate everything that you're going to display on the web page, but that doesn't mean that that data can't be structured inside lists or custom strongly typed objects. Iterating over collections and calling simple getters from a template is fine.
 
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
ScroogeXHTML 8.2 - easy to use RTF to HTML converter library
https://coderanch.com/t/707504/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!