thymeleaf
Thymeleaf is a java web template engine designed for XML/XHTML/HTML5.
Thymeleaf: java XML/XHTML/HTML5 template engine
I have the following code in one of my controllers:
@Controller
@RequestMapping("/preference")
public class PreferenceController {
@RequestMapping(method = RequestMethod.GET, produces = "text/html")
public String preference() {
return "preference";
}
I am simply trying to test it using Spring MVC test as follows:
@ContextConfiguration
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class PreferenceControllerTest {
@Autowired
private WebApplicationContext ctx;
private MockMvc mockMvc;
@Before
public void setup() {
mockMvc = webAppContextSetup(ctx).build();
}
@Test
public void circularViewPathIssue() throws Exception {
mockMvc.perform(get("/preference"))//
.andDo(print());
}
I am getting the following exception:
Circular view path [preference]: would dispatch back to the current
handler URL [/preference] again. Check your ViewResolver setup! (Hint:
This may be the result of an unspecified view, due to default view
name generation.)
What I find strange is that it works fine when I load the "full" context configuration that includes the template and view resolvers as shown below:
<bean class="org.thymeleaf.templateresolver.ServletContextTemplateResolver" id="webTemplateResolver">
<property name="prefix" value="WEB-INF/web-templates/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="characterEncoding" value="UTF-8" />
<property name="order" value="2" />
<property name="cacheable" value="false" />
</bean>
I am well aware that the prefix added by the template resolver ensures that there are not "circular view path" when the app uses this template resolver.
But then how I am supposed to test my app using Spring MVC test? Has anyone got any clue?
Source: (StackOverflow)
Is there a way to do a loop in Thymeleaf without a list?
I'd like to essentially convert this snippet to Thymeleaf:
<jsp:useBean id="now" class="java.util.Date" />
<fmt:formatDate var="year" value="${now}" pattern="yyyy" />
<c:forEach var="i" begin="0" end="99">
<form:option value="${year-i}" />
</c:forEach>
</form:select>
-- Update --
I've decided this is along the lines of how I want to do it, but I'm not sure about the springEL syntax:
<option th:each="i : ${#numbers.sequence( 1, 100)}" th:value="#{ T(java.util.Date).getYear() - $i }">1</option>
Source: (StackOverflow)
I searched first but I found confusing answers since I'm new to Thymeleaf and amateurish at best at javascript.
I just want to know how to pass variable expressions into javascript functions, sort of like in JSP:
<a rel='nofollow' href="#" onclick="javascript:getContactId('${contact.id}');">Button</a>
Of course, this fails with Thymeleaf and passes the string ${contact.id} instead of its value, so how could I get the value of the variable expression instead?
The reason I want it this way is because it depends on the row which is being iterated by th:each
.
If there's no other way except to use th:inline
, then what's the best approach considering the above statement?
Source: (StackOverflow)
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 class="form-control"
type="text"
th:value="${client.name}" //this line is ignored
th:field="*{clientName}"/>
</div>
form is DTO object and client is Entity object from database.
What is the correct way to solve this situation?
By not working I mean - lets say that initial values are client.name="Foo" and form.clientName=null.
I need that input field display value is "Foo" and after form submission form.clientName value to become "Foo". But the input field displays nothing and on submission form.clientName value still is null;
Source: (StackOverflow)
Thymeleaf puts a large emphasis on "natural templating", which means that all templates are already valid XHTML files. I always thought that is a great step forward that I can generate fragments in my templates e.g. in JSP I'd write
<tagfile:layout title="MyPageTitle">
<jsp:body>
Main content goes here
</jsp:body>
</tagfile:layout>
My "Layout"-Tagfile contains all the header-tags (title, link to stylesheets,...), the menu and justs inserts title text and body at the right point. I don't need to know anything about stylesheets menus or the like when designing my html fragement.
This is in contrast to the idea of Thymeleaf which encourages me to create full html pages (including a sample menu and all the headers). While the manual of Thymeleaf continues to emphasise how great this is, it never deals with duplication of code concerns:
- I have one template that generates a menu and all my other templates (could be many) include a copy&pasted dummy menu just so that I can view the template in a browser without the server side generation mechanism. If I have 100 templates that means that prossibly the exact same dummy menu exists 100x (in each and every template). If I change the look of the menu it's not done with creating a new dummy menu, but I need to copy&paste the new dummy menu into 100 templates.
- Even if I decide to do something as simple as renaming my CSS file I need to touch all my templates as well.
- There is always the danger that my template looks just fine in my browser, but the generated output is broken because... well... I broke it (could be as simple as a misspelled variable name). Thus I will need to test the output with the actual generation anyway.
Did I misunderstand something there? Or is this indeed a trade-off? How do you minimize the impact of code duplication?
Source: (StackOverflow)
I am writing a template which should render list of employees. Employees are passed to Thymeleaf in form of list of departments where each department has its own list of employees.
As my task is to display them all - the problem is to handle continous numeration. Each employee should appear as a row with next number.
Following attempt allows to index employees of given department, with new numeration per department:
<table th:each="department, depStatus : departmentList">
<tr th:each="employee, empStatus : department.employees">
<td th:inline="text">[[${empStatus.index+1}]]</td>
But my point is to keep continous numeration through all departments, just like this:
1. Employee A from Dept X
2. Employee B from Dept X
3. Employee C from Dept Y
I know I can make this structure flat at server side, but I cannot believe that it is the only way.
I have also tried to intruduce local variable with th:with="idx = 0"
and then increment it somewhere with th:with="idx = ${idx} + 1
, but this simply overrides outter idx
value.
Source: (StackOverflow)
Let's say I have two Thymeleaf templates:
index.html:
<!DOCTYPE html>
<html>
<head></head>
<body>
<header>foo</header>
<section>
<div th:replace="fragments/main :: content"></div>
</section>
<footer>bar</footer>
</body>
</html>
fragments/main.html:
<!DOCTYPE html>
<html>
<head></head>
<body>
<div th:fragment="content">
<p>This is the main content.</p>
</div>
</body>
</html>
How do I prevent Tymeleaf from including the div
that defines the fragment in the composited output? That is, how do I get Thymleaf to generate the following output:
<!DOCTYPE html>
<html>
<head></head>
<body>
<header>foo</header>
<section>
<p>This is the main content.</p>
</section>
<footer>bar</footer>
</body>
</html>
Instead of:
<!DOCTYPE html>
<html>
<head></head>
<body>
<header>foo</header>
<section>
<div>
<p>This is the main content.</p>
</div>
</section>
<footer>bar</footer>
</body>
</html>
Source: (StackOverflow)
I have searched hard for a syntax like the JSTL one ${pageContext.request.contextPath}
,
I did made a javascript code to change the action attribute on the form to call the edit method on the spring controller so, the problem is the below code dont work without calling the Context first like ${pageContext.request.contextPath}/edit.html
<script th:inline="javascript">
function edit() {
document.getElementById("user_form").action = "/edit.html";
}
</script>
so what is the Syntax to call Thymeleaf context path?
Source: (StackOverflow)
I'm new to thymeleaf and I have the following template code to format a date. There are times when that date will be null in the returned object. What is the best way to check for null in thymeleaf in this situation? Currently the template is throwing the following error?
Caused by: java.lang.IllegalArgumentException: Cannot apply format on null
at org.thymeleaf.util.Validate.notNull(Validate.java:37)
at org.thymeleaf.util.DateUtils.format(DateUtils.java:182)
at org.thymeleaf.expression.Dates.format(Dates.java:164)
Source: (StackOverflow)
I'm new in thymeleaf and I try to create a template. My problem is this code:
CODE
<h1 th:text="${header.title}" >
title
<small th:text="${header.subtitle}" >Subtitle</small>
</h1>
I want to get this output:
<h1> TITLE <small> SUBTITLE</small> </h1>
But this is the real output:
<h1> TITLE </h1>
How can I do so it doesn't remove what is inside of "small"?.
Thanks in advance.
Source: (StackOverflow)
I am new to thymeleaf and am converting ma web page from jsp to thymeleaf. I have a strut tag like this
<c:set var="someVariable" value="${someValue}"/>
which that the variable can be used anywhere in jsp. Is there any such alternatives for this in thymeleaf?
Source: (StackOverflow)
The exception is
system Cannot find System Java Compiler.
Ensure that you have installed a JDK (not just a JRE) and configured your JAVA_HOME system variable to point to the according directory.
Could not execute build using Gradle installation 'C:\gradle-1.2-all\gradle-1.2'.
But I have installed jdk1.7 and set the environment variable also.
Source: (StackOverflow)
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.value}]]</span></td>
Where from DAO abc I am reading the currency value, it should be formatted.
Currently printing $ 1200000.0 it should print $ 1,200,000.0 .0
Source: (StackOverflow)
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 simple and clear toturial on how to do simple Ajax request to controller and in result rendering only a part of a template (fragment).
My app has everything configured (Spring 3.2, spring-security, thymeleaf, ...) and works as expected. Now i would like to do Ajax request (pretty simple with jQuery but i don't wan't to use is since Thymeleaf in its tutorial, chapter 11: Rendering Template Fragments (link) mentiones it can be done with fragments.
Currently i have in my Controller
@RequestMapping("/dimensionMenuList")
public String showDimensionMenuList(Model model) {
Collection<ArticleDimensionVO> articleDimensions;
try {
articleDimensions = articleService.getArticleDimension(ArticleTypeVO.ARTICLE_TYPE);
} catch (DataAccessException e) {
// TODO: return ERROR
throw new RuntimeException();
}
model.addAttribute("dimensions", articleDimensions);
return "/admin/index :: dimensionMenuList";
}
the part of the view where i would like to replace
menu items:
<ul th:fragment="dimensionMenuList" class="dropdown-menu" >
<li th:unless="${#lists.isEmpty(dimensions)}" th:each="dimension : ${dimensions}">
<a rel='nofollow' href="#" th:text="${dimension.dimension}"></a>
</li>
</ul>
Any clue is greatly appreciated. Especially if i don't have to include any more frameworks. Its already too much for java web app as it is.
Source: (StackOverflow)
Is there a simple way in thymeleaf to show the content of an attribute property if the property and the attribute exist? If there's an attribute "error" with a property "summary" in my html page, I'd like to show it:
<span th:text="${error.summary}">error summary</span>
If there is no attribute "error" the following error is raised:
org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Field or property 'summary' cannot be found on null
Currently I'm using the following approach, which just seems too complicated.
<span th:if="${error != null and error.summary != null}"><span th:text="${error.summary}">error summary</span></span>
Is there a simpler way to achieve that?
Source: (StackOverflow)