Skip to main content

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
Filter by
Sorted by
Tagged with
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>...
Maciej Ziarko's user avatar
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/...
balteo's user avatar
  • 24.3k
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 ...
Matteo's user avatar
  • 1,943
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-...
Alexandru Severin's user avatar
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 ...
vdenotaris's user avatar
  • 13.5k
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 ...
serkan's user avatar
  • 1,237
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 ...
NeilA's user avatar
  • 1,500
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 ...
Alexandru Severin's user avatar
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 ...
Rahul Raj's user avatar
  • 3,359
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&...
JohnPortella's user avatar
  • 1,811
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 ...
phil.e.b's user avatar
  • 2,601
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 ...
Chris's user avatar
  • 943
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....
Deepak Ramakrishnan Kalidass's user avatar
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 ...
Mike3355's user avatar
  • 11.8k
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 ...
adam.shaleen's user avatar
  • 1,061
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 ...
Hoffmann's user avatar
  • 14.7k
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 ...
benscabbia's user avatar
  • 17.9k
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} &gt; 50" th:if="${evaluation} &lt; 30" ...
brnrd's user avatar
  • 3,466
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 ...
user avatar
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 ...
y0gie007's user avatar
  • 727
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})}"&...
richersoon's user avatar
  • 4,832
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 ...
user3364391's user avatar
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 ...
Xipo's user avatar
  • 1,803
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.
user3128455's user avatar
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 ...
Andremoniy's user avatar
  • 34.6k
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 ...
Tobou's user avatar
  • 491
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,...
davioooh's user avatar
  • 24.4k
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 ...
brunoid's user avatar
  • 2,161
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....
giri's user avatar
  • 27.1k
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 ...
Kleber Mota's user avatar
  • 8,795
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:...
Shuhan Liu's user avatar
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=&...
tomdavies's user avatar
  • 1,956
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 ...
Andrea's user avatar
  • 16.4k
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 ...
Marcel Overdijk's user avatar
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 ...
Romain Linsolas's user avatar
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 -->...
occurred's user avatar
  • 510
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" ...
Liem Do's user avatar
  • 953
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 ...
Lucca's user avatar
  • 1,507
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 ...
mtyurt's user avatar
  • 3,429
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 { ...
stevecross's user avatar
  • 5,646
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 ...
Anil Kumar Pandey's user avatar
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 ...
Sumit Rathore's user avatar
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&...
Lazar Lazarov's user avatar
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 ...
QoP's user avatar
  • 28.2k
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("...
Revansha's user avatar
  • 2,013
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?
codingbash's user avatar
  • 1,142
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 ...
undefined's user avatar
  • 3,614
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...." ...
Daividh's user avatar
  • 405
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?
Dave's user avatar
  • 1,482
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 '...
Faraj Farook's user avatar
  • 14.7k

1
2 3 4 5
187