I have a form:select that is populated using enum values and I want to make sure that at least one of the options is selected (the user is not allowed to deselect all).
<form:select path="statusArray" id="status" multiple="true">
<form:options items="${availableStatuses}" itemLabel="displayName" itemValue="value"/>
</form:select>
How can I do that ?
Note: I am fairly new to Spring MVC and to web development in general.
Thanks!
You can change variables to yours, its working staff below:
<select th:field="*{percentage}">
<option th:each="i : ${#numbers.sequence(0, 100)}" th:value="${i}" th:text="${i}"
th:selected="${i==75}"></option>
</select>
I don't think spring mvc provides the feature to get selected one or more options in <form:select
while rendering the view, But one work around is using JSTL condition tag you can achieve it.
Below example shows you how to make selected the options whose value 1 or 3:
<form:select path="statusArray" id="status" multiple="true">
<c:forEach items="${availableStatuses}" var="opt">
<c:choose>
<c:when test="${opt.value eq 1 or opt.value eq 3}">
<option value="${opt.value}"
selected="selected"><c:out value="${opt.displayName}"/></option>
</c:when>
<c:otherwise>
<option value="${opt.value}"><c:out value="${opt.displayName}"/></option>
</c:otherwise>
</c:choose>
</c:forEach>
</form:select>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.