• 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
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Inserting raw HTML into Thymeleaf template  RSS feed

 
Rancher
Posts: 1170
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: 10105
212
  • 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
Rancher
Posts: 1170
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: 10105
212
  • 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
Rancher
Posts: 1170
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: 10105
212
  • 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.
 
You don't like waffles? Well, do you like this tiny ad?
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!