Issue
I have a dropdown list, status: having 4 states( Estimated, budgetted, processed, finished). Each unique ID has a possibility to have one of these these 4 status values I use thymeleaf to display all the status values dynamically.
Now the requirement is to open a particular ID, and then update the status. BEfore doing so, it should show the current value of the status( from the db).
I pass the required information from my controller class:
controller.java
StatusTable st = new StatusTable();
List<String>statusValues = st.getAllStatus();
String status = st.getStatus(100); //Herre the status is processed
model.addAttribute("statusdef", statusval)
model.addAttribute("status", statusValues )
return "form.html";
form.html
<html xmlns:th="https://www.thymeleaf.org"
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<body>
<div th:fragment="editdata">
<div class="container-md">
<a style="font-weight: bold">Edit the Status</a>
<div class="row justify-content-md-end">
<form>
<div class="row">
<div class="col-2">
<a>Status</a>
</div>
<div class="col-3">
<select class="form-select" aria-label="Default select example"
name="status" th:selected ="${statusdef}" required>
<option th:each="statusvalue : ${status}"
th:text="${statusvalue}">
</select>
</div>
</div>
</form>
</div>
</div>
</body>
When I do this, I see that it does not show the default value (processed), it shows the firstvalue (Estimated) I want it to show "processed".. Any suggestions?
Solution
You are using th:seleceted at wrong place, it must be with option tag and must evaluate to true, like below -
<select class="form-select" aria-label="Default select example"
name="status" required>
<option th:each="statusvalue : ${status}"
th:selected ="${statusdef==statusvalue}"
th:text="${statusvalue}">
</option>
</select>
Answered By - tomar
Answer Checked By - Terry (JavaFixing Volunteer)