Briefly, when users submit a Thymeleaf form, the URL looks like this:
http://localhost:8080/equipment/1?%5C_searchTerms=on&searchTerms=dumbbell&%5C_searchTerms=bench&%5C_searchTerms=on&%5C_searchTerms=on&%5C_searchTerms=on&%5C_searchTerms=on&%5C_searchTerms=on&%5C_searchTerms=on&%5C_searchTerms=on&%5C_searchTerms=on&%5C_searchTerms=on&%5C_searchTerms=on&%5C_searchTerms=on&%5C_searchTerms=on&%5C_searchTerms=on&%5C_searchTerms=on&%5C_searchTerms=on&%5C_searchTerms=on
When I am expecting it to look something like this:
http://localhost:8080/equipment/1?searchTerms=dumbbell&searchTerms=bench
To be more detailed, I have a Thymeleaf form which allows users to select different gym equipment via checkbox. The options that users may select on this form are generated with a th:each Thymeleaf statement.
<form method = "GET" action="#" th:action = "@{/equipment/1}" th:object="${inputWrapper}">... <tbody><tr th:each="equipment : ${equipmentList}"><td><input type="checkbox" th:field="*{searchTerms}" th:value="${equipment.name}" </td></tr></tbody>...</form>
equipmentList
is a list of Equipment
objects. The name
field of Equipment
objects holds, understandably, the name of the equipment. Example names are "bench", "dumbbell," or "jump rope."
searchTerms
is a String ArrayList inside the object inputWrapper
. When the user selects checkboxes and submits the form, Thymeleaf populates searchTerms
with the selected user input. This translates in the URL as something like searchTerms=[input selected by user]. For example, searchTerms=dumbbell. In this regard, everything is working as expected. (Object binding inside the controller is working as expected too.)
The unexpected part is that unselected checkbox values are also showing up in the URL, and the value is simply "on." I understand that "on" is the default value for an unchecked checkbox [x
], but my understanding from that documentation is also that unchecked values are not sent to the server. (For what it's worth, inside the controller, the unchecked objects are not part of the searchTerms
list.)
Now, I notice that for the unselected values showing up in the URL, there is an underscore in front of them. For example, _searchTerms=on.
The Thymeleaf documentation says in its Inputs section for checkboxes:
Don’t worry about those hidden inputs with
name="_features"
: they are automatically added in order to avoid problems with browsers not sending unchecked checkbox values to the server upon form submission.
Surely this can't mean that Thymeleaf intends for the URLs to look like this. What can be done to make the URL include only the selected items as is the standard?
I would like the input to be visible in the URL, which is why I am using a GET request.
I have also checked some other questions with Thymeleaf lists, but they are all not resolving the issue or are dealing with POST requests and are therefore not considering this issue:
thymeleaf-thfield-overwrites-thvalue
- (Note: This solution technically works, except I am looking for user input to be bound to a list specifically)
spring-mvc-thymeleaf-form-binding-with-an-arraylist
- (Note: This just made the URL look like http://localhost:8080/equipment/1?%5C_searchTerms=on&searchTerms=dumbbell&%5C_searchTerms=bench&%5C_searchTerms%5C%5B2%5C%5D=on&%5C_searchTerms%5C%5B3%5C%5D=on&%5C_searchTerms%5C%5B4%5C%5D=on ....)
I have a (tiny) background in Spring Boot APIs and batch processing, so form submissions is newish to me.
Thank you!