Reputation: 378
I have currently written code with a Game object that has firstNameWhite, lastNameWhite, firstNameBlack and lastNameBlack instance variables. I have written the following html to have a drop downmenu that contains full names and four separate fields to populate the four fields up above. I want the code to use the four fields if the dropdown menu has not been selected, but it splits up the full names in the dropdown menu to populate the four field when a dropdown name has been selected. Currently I am getting the exception
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression:
"[[${whiteplayername} != null && !empty ${whiteplayername}]]" (template: "submitgame"
- line 95, col 28)
I know how to do this in the java side of the code, but I am looking for a way to split and populate in the html. Here is my code
<form th:object="${game}" th:action="@{/addgame}" th:method="post">
<label for="whiteplayername">Select first name:</label>
<label for="whiteplayername">Select first name:</label>
<select name="whiteplayername" id="whiteplayername" th:object="${names}">
<th:block th:each="name : ${names}">
<option th:value="${name.firstNameWhite} + ' ' + ${name.lastNameWhite}"
th:text="${name.firstNameWhite} + ' ' + ${name.lastNameWhite}"></option>
</th:block>
</select>
<label for="blackplayername">Select white player:</label>
<select name="blackplayername" id="blackplayername" th:object="${names}">
<th:block th:each="name : ${names}">
<option th:value="${name.firstNameBlack} + ' ' + ${name.lastNameBlack}"
th:text="${name.firstNameBlack} + ' ' + ${name.lastNameBlack}"></option>
</th:block>
</select><br><br>
<label for="firstnamewhite">First name white:</label>
<input type="text" id="firstnamewhite" th:field="*{firstNameWhite}" />
<input type="text" th:unless="[[${whiteplayername} != null && !empty ${whiteplayername}]]" th:value="${whiteplayername.split(' ')[0]}"/>
<input type="text" id="lastnamewhite" th:field="*{lastNameWhite}" />
<input type="text" th:unless="[[${whiteplayername} != null && !empty ${whiteplayername}]]" th:value="${whiteplayername.split(' ')[0]}"/>
<input type="text" id="firstnameblack" th:field="*{firstNameBlack}" />
<input type="text" th:unless="[[${blackplayername} != null && !empty ${blackplayername}]]" th:value="${blackplayername.split(' ')[0]}"/>
<input type="text" id="lastnameblack" th:field="*{lastNameBlack}" />
<input type="text" th:unless="[[${blackplayername} != null && !empty ${blackplayername}]]" th:value="${blackplayername.split(' ')[0]}"/>
<label for="date">Date:</label><br>
<input type="text" id="date" th:field="*{date}">
<input type="submit" value="Submit">
</form>
NB: names is a list of Game objects
Upvotes: 1
Views: 688
Reputation: 22042
The Thymeleaf [[ ... ]]
syntax is used for expression inlining, where you want to use a Thymeleaf expression directly in a piece of text - not using a Thymeleaf th:
atttribute.
In your case you should therefore remove the [[
and ]]
from your template.
You also need to replace the Java &&
with Thymeleaf and
- see the standard expression syntax overview for a summary of this and other operators.
For the empty
operation, you can use ${whiteplayername.isEmpty()}
or the more concise ${whiteplayername.empty}
Putting these all together gives us the following (you can use !
or not
for negation):
<!-- PROBABLY NOT WHAT YOU WANT -->
<input type="text"
th:unless="${whiteplayername} != null and not ${whiteplayername.empty}"
th:value="${whiteplayername.split(' ')[0]}"/>
However, I am not sure this makes sense. You will not see any select boxes with this logic, if there is a valid player name. If you really want to use th:unless
, then I think you should be using the following logic, which is the reverse of what you have:
<!-- PROBABLY WHAT YOU WANT, USING TH:UNLESS -->
<input type="text"
th:unless="${whiteplayername} == null or ${whiteplayername.empty}"
th:value="${whiteplayername.split(' ')[0]}"/>
This works with an input such as
model.addAttribute("whiteplayername", "John Smith");
to generate the following HTML:
<input type="text" value="John">
And in that case, if that is what you do actually want, then why not use th:if
instead of th:unless
- which I think is clearer:
<!-- PROBABLY WHAT YOU WANT, USING TH:IF -->
<input type="text"
th:if="${whiteplayername} != null and not ${whiteplayername.empty}"
th:value="${whiteplayername.split(' ')[0]}"/>
In these last two examples, the input
element is not created if the name is null or an empty string.
Upvotes: 1