Questions tagged [thymeleaf]
Thymeleaf is an XML/XHTML/HTML5 template engine (extensible to other formats) that can work both in web and non-web environments. It is better suited for serving XHTML/HTML5 at the view layer of web applications, it can process any XML file even in offline environments. It provides an optional module for integration with Spring MVC, so that you can use it as a complete substitute of JSP in your applications made with this technology, even with HTML5.
thymeleaf
9,312
questions
170
votes
14
answers
458k
views
How to do if-else in Thymeleaf?
What's the best way to do a simple if-else in Thymeleaf?
I want to achieve in Thymeleaf the same effect as
<c:choose>
<c:when test="${potentially_complex_expression}">
<h2>...
166
votes
26
answers
314k
views
How to avoid the "Circular view path" exception with Spring MVC test
I have the following code in one of my controllers:
@Controller
@RequestMapping("/preference")
public class PreferenceController {
@RequestMapping(method = RequestMethod.GET, produces = "text/...
162
votes
10
answers
237k
views
Setting up a JavaScript variable from Spring model by using Thymeleaf
I am using Thymeleaf as template engine. How I pass a variable from Spring model to JavaScript variable?
Spring-side:
@RequestMapping(value = "message", method = RequestMethod.GET)
public String ...
149
votes
3
answers
124k
views
Using data-* attribute with Thymeleaf
Can I set data-* attribute with Thymeleaf?
As I understood from Thymeleaf documentation I tried:
<div th:data-el_id="${element.getId()}"> <!-- doesn't work -->
<div data-th-...
119
votes
10
answers
157k
views
Thymeleaf: how to use conditionals to dynamically add/remove a CSS class
By using Thymeleaf as template engine, is it possible to add/remove dynamically a CSS class to/from a simple div with the th:if clause?
Normally, I could use the conditional clause as follows:
<a ...
114
votes
11
answers
221k
views
Using Thymeleaf when the value is null
I have some values in my database which can be null if they have not already been entered.
But when I use Thymeleaf in my html, it gives an error when parsing null values.
Is there any way to handle ...
104
votes
5
answers
148k
views
Thymeleaf: Concatenation - Could not parse as expression
I'm having an issue when trying to concat multiple values in my template.
According to Thymeleaf here I should simply be able to + them together...
4.6 CONCATENATING TEXTS
Texts, no matter ...
102
votes
3
answers
91k
views
Process Thymeleaf variable as HTML code and not text
I'm using Thymeleaf to process html templates, I understood how to append inline strings from my controller, but now I want to append a fragment of HTML code into the page.
For example, lets stay that ...
78
votes
4
answers
121k
views
How to check if list is empty using thymeleaf?
<div th:if="${tblUserList != null}">
--content--
</div>
The above thymeleaf code is not working, where tblUserList is a list. So I want to check whether the list is empty instead of ...
77
votes
4
answers
157k
views
Thymeleaf th:text - Put a text without removing HTML structures
I'm new in thymeleaf and I try to create a template. My problem is this code:
<h1 th:text="${header.title}" >
title
<small th:text="${header.subtitle}" >Subtitle&...
75
votes
2
answers
93k
views
How to loop through Map in Thymeleaf
I am trying to understand how to loop through all entries in a Map in Thymeleaf. I have a domain object being processed by Thymeleaf that contains a Map.
How do I loop through the keys and fetch the ...
70
votes
5
answers
117k
views
Thymeleaf construct URL with variable
I have the following code setting a variable in my controller:
model.set("type", type);
In the thymeleaf view I want to construct a form with action url:
/mycontroller/{type}
Any ideas how to ...
65
votes
3
answers
130k
views
setting up a value for a variable name in thymeleaf
I am new to Thymeleaf and converting my Web page from JSP to Thymeleaf. I have a strut tag like this:
<c:set var="someVariable" value="${someValue}"/>
That variable can be used anywhere in JSP....
63
votes
23
answers
324k
views
Error resolving template "index", template might not exist or might not be accessible by any of the configured Template Resolvers
This question has been asked before but I did not solve my problem and I getting some weird functionality.
If I put my index.html file in the static directory like so:
I get the following error in my ...
62
votes
6
answers
120k
views
Formatting date in Thymeleaf
I'm brand new to Java/Spring/Thymeleaf so please bear with my current level of understanding. I did review this similar question, but wasn't able to solve my problem.
I'm trying to get a simplified ...
61
votes
3
answers
43k
views
How to show localization messages with parameters in Spring 3 / Thymeleaf
I'm using Spring 3 and Thymeleaf to make some webpages and I am lost as for how to show messages like this:
welcome.message=Hello {0}, welcome!
and then replace {0} with the user name inside ...
61
votes
2
answers
112k
views
How to bind an object list with thymeleaf?
I am having a lot of difficulty with POSTing back a form to the controller, which should contain simply an arraylist of objects that the user may edit.
The form loads up correctly, but when it's ...
59
votes
4
answers
120k
views
How to have multiple condition in an th:if tag using thymeleaf
I have a text to render in three different possible colors using thymeleaf.
So the code I've made so far to test the value is:
th:if="${evaluation} > 50"
th:if="${evaluation} < 30"
...
58
votes
5
answers
28k
views
How to add source code comments in Thymeleaf templates that don't get included in generated HTML?
I am using full stack Thymeleaf (spring mvc, security, layout dialect, webflow) in a mid-size web application.
Ok..now that we put so much of code in the html templates it would be nice to include ...
58
votes
3
answers
75k
views
Spring MVC 3.2 Thymeleaf Ajax Fragments
I'm building application with Spring MVC 3.2 and Thymeleaf templating engine. I'm a beginner in Thymeleaf.
I have everything working, including Thymeleaf but I was wondering if anyone knows of a ...
57
votes
3
answers
125k
views
Thymeleaf - How to loop a list by index
How can I loop by index?
Foo.java
public Foo {
private List<String> tasks;
...
}
index.html
<p>Tasks:
<span th:each="${index: #numbers.sequence(0, ${foo.tasks.length})}"&...
53
votes
5
answers
63k
views
Thymeleaf, fragments and default parameters
I have created fragments.html file. It contains the following fragment:
<div th:fragment="my_fragment(content)">
<p th:text="${content}"></p>
</div>
I put the above ...
52
votes
12
answers
71k
views
Spring Security hasRole() not working
I'm facing a problem when using Spring Security && Thymeleaf, specifically when trying to use the hasRole expression. The 'admin' user has a role 'ADMIN' but hasRole('ADMIN') resolves to false ...
50
votes
10
answers
48k
views
Comparing the enum constants in thymeleaf
I have an enum, Constants:
enum Constants {
ONE,TWO,THREE;
}
How can I compare the enum Constants in Thymeleaf.
Thanks.
48
votes
2
answers
114k
views
For each operator in Thymeleaf
I can not find syntax for building simple for-each-loop in Thymeleaf template.
I'm not satisfied with just th:each="" attribute, because it copies the tag in which it's located.
What I'm ...
48
votes
7
answers
213k
views
How to set thymeleaf th:field value from other variable
I have a simple text input field where i have to set default value from one object and save its final value in other. The following code is not working.
<div th:object="${form}">
<input ...
47
votes
2
answers
46k
views
Choosing between Thymeleaf and Angular for a new Spring MVC project [closed]
I'm starting a new Spring Boot web application and I need to make a choice about how to implement front-end.
I'm really comfortable with Thymeleaf templating framework that I used in several projects,...
46
votes
9
answers
124k
views
Spring Boot and custom 404 error page
In my Spring Boot application, I'm trying to configure custom error pages, for example for 404, I have added a following Bean to my application configuration:
@Bean
public ...
46
votes
4
answers
90k
views
How to format the currency in HTML5 with thymeleaf
I am stuck with formatting the currency in HTML 5. I have application where I have to format the currency. I have below code snippet
<td class="right"><span th:inline="text">$ [[${abc....
43
votes
8
answers
119k
views
Using thymeleaf variable in onclick attribute
In my current spring-boot project, I have one view with this html code:
<button type="button" class="btn btn-primary" onclick="upload()" th:utext="#{modal.save}"></button>
in the onclick ...
42
votes
16
answers
75k
views
Spring boot + thymeleaf in IntelliJ: cannot resolve vars
I'm writing a short web form application using spring boot and thymeleaf on IntelliJ, but it seems that in the html file, all fields in the model cannot be resolved. Here is my code:
Controller class:...
41
votes
4
answers
36k
views
Thymeleaf with Spring Security - how to check if user is logged in or not?
I'm using Spring Boot with Thymeleaf and Spring Security.
I've got a simple view with a login link. When the user logs in, I'd like to change login link to logout link.
I tried:
<div sec:authorize=&...
40
votes
4
answers
101k
views
Thymeleaf: check if a variable is defined
How can I check if a variable is defined in Thymeleaf?
Something like this in Javascript:
if (typeof variable !== 'undefined') { }
or this in PHP:
if (isset($var)) { }
Is there an equivalent in ...
40
votes
3
answers
70k
views
Thymeleaf not displaying Spring form error messages
I'm migrating a Spring jsp application to Thymeleaf but having problems displaying form errors.
I'm using the SpringTemplateEngine and ThymeleafViewResolver and rendering of templates works.
Also ...
40
votes
4
answers
34k
views
Thymeleaf templates - Is there a way to decorate a template instead of including a template fragment?
I am working with Thymeleaf for the first time, and I need a clarification about the templates. If I correctly understand the documentation, I can include a template - or just a fragment of it - in my ...
39
votes
2
answers
17k
views
Get spring application environment in thymeleaf
My Spring Boot application runs with 3 configurations:
application.properties --> for development environment
application-test.properties --> for test environment
application-production.properties -->...
37
votes
6
answers
53k
views
Thymeleaf multiple submit button in one form
I have a fragment of HTML page with one form and 2 button:
<form action="#" data-th-action="@{/action/edit}" data-th-object="${model}" method="post">
<button type="submit" name="action" ...
37
votes
1
answer
46k
views
Whats the default path for static files in Angular2?
I'm currently working on a small Angular2 project. In my case i have to reroute some of the requests (like "/faq", "/aboutus") back to my old backend server to get some server side rendered thymeleaf ...
36
votes
8
answers
111k
views
Thymeleaf - How to add checked attribute to input conditionally
As you know, input component has an attribute, checked to whether mark the checkbox as enabled by default or not.
<input type="checkbox" name="mycheckbox" checked="checked"/>
To disable the ...
35
votes
4
answers
43k
views
How to display all possible enum values in a dropdown list using Spring and Thymeleaf?
I have a domain object that has an enum property and I want to display a dropdown list with all possible enum values in the form for this object. Imagine the following object:
public class Ticket {
...
35
votes
5
answers
62k
views
Spring MVC: How do I get current url in Thymeleaf
I am using Thymeleaf Template Engine with Spring Web MVC and I am got stuck while creating url's with the help of current url. Is there any way to get current inside Thymeleaf HTML file? eg: Suppose ...
35
votes
8
answers
87k
views
How to use "map.get(key)" in Thymeleaf - Broadleaf Ecom
I have a Hashmap (String, List<Offers>), passed to a Thymeleaf page. I am getting this map on the page and I can access it.
How can I do map.get(key) with Thymeleaf? I just need to fetch the ...
34
votes
3
answers
60k
views
How to set the id attribute of HTML element dynamically with Thymeleaf
Lets say I have an object : ${object}
and I have the following form:
<form id="{{'myForm' + object.id}" class="some class"
th:action="@{/doSomething}" method="post">
....
</form&...
33
votes
4
answers
67k
views
SpringBoot - UTF-8 Doesnt work in messages.properties
¿What's the problem?
I can't display in UTF-8 the messages I got in messages.properties.
An example
<h1 id="logo">Electrónico</h1>
this works okay but when I try to use my message ...
33
votes
5
answers
111k
views
Spring Boot - redirect to a different controller method
I am creating a very basic application with SpringBoot and Thymeleaf. In the controller I have 2 methods as follows:
Method1 - This method displays all the data from the database:
@RequestMapping("...
33
votes
4
answers
37k
views
Difference between Thymeleaf include and replace?
What is the difference between the two Thymeleaf attributes: th:include and th:replace?
32
votes
10
answers
170k
views
Inserting an image from local directory in thymeleaf spring framework (with maven)
I have developed a project using this link: https://spring.io/guides/gs/serving-web-content/ I used maven to develop above project.
I have two html files under this. abc.html and xyz.html. To insert ...
32
votes
8
answers
46k
views
Thymeleaf + Spring : How to keep line break?
I'm using Thymeleaf template engine with spring and I'd like to display text stored throught a multiline textarea.
In my database multiline string are store with "\n" like this : "Test1\nTest2\n...."
...
31
votes
1
answer
24k
views
Thymeleaf - The difference between th:field="${}" and th:field="*{}"
I can't find on the tutorial the difference between this two instructions.
th:field="${something}" and th:field="*{something}"
Can anyone show me some example?
31
votes
3
answers
60k
views
Thymleaf switch statement with multiple case
In Short
I want to have switch statement in thymeleaf with logic once written to multiple case statements.
In detail
I want to implement this in the thymeleaf
switch(status.value){
case '...