thymeleaf select 回显
Thymeleaf is a Java-based templating engine that allows you to create dynamic web pages and HTML templates. To perform select element value retrieval and rendering in Thymeleaf, you can use the `th:field` and `th:selected` attributes.
Here's an example of how to use Thymeleaf to select the previously selected value in the HTML select element:
1. In your controller, pass the selected value to the model:
```java
model.addAttribute("selectedValue", "option2"); // Replace "option2" with the actual selected value from your data source
```
2. In your HTML template, use Thymeleaf to render the select element by using the `th:fielthyme
d` and `th:selected` attributes:
```html
<select th:field="*{selectedValue}">
<option value="option1" th:selected="${selectedValue == 'option1'}">Option 1</option>
<option value="option2" th:selected="${selectedValue == 'option2'}">Option 2</option>
<option value="option3" th:selected="${selectedValue == 'option3'}">Option 3</option>
</select>
```
In the above example, the `th:field` attribute is used to bind the select element's value to the `selectedValue` variable in the model. The `th:selected` attribute is used to conditionally select the option based on the value of `selectedValue` in the model.
Replace "option1", "option2", and "option3" with the actual values you want to display in the select options, and adjust the condition in the `th:selected` attribute accordingly.
This way, the select element will be populated with the options, and the previously selected value will be displayed when the page is rendered.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论