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

Inserting raw HTML into Thymeleaf template  RSS feed

 
Master Rancher
Posts: 1061
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: 9707
192
  • 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: 1061
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: 9707
192
  • 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: 1061
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: 9707
192
  • 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.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!