thymeleaf radio
时间: 2023-10-13 21:21:02 浏览: 345
Thymeleaf is a server-side Java template engine that can be used for building web applications. Radio buttons are a common form element that allows users to select one option from a list of options. To create radio buttons using Thymeleaf, you can use the `th:each` and `th:field` attributes.
Here's an example:
```
<form>
<div th:each="fruit : ${fruits}">
<input type="radio" th:field="*{selectedFruit}" th:value="${fruit}" />
<label th:text="${fruit}"></label>
</div>
</form>
```
In this example, `fruits` is a list of fruit options that you want to display as radio buttons. The `th:each` attribute is used to iterate over the list and create a radio button for each option.
The `th:field` attribute is used to bind the selected option to a model attribute named `selectedFruit`. This means that when the form is submitted, the selected option will be assigned to the `selectedFruit` attribute in the model.
Finally, the `th:value` attribute is used to set the value of each radio button to the corresponding fruit option, and the `th:text` attribute is used to display the fruit option label next to each radio button.
阅读全文