[ACCEPTED]-Set selected option on existing select tag with jstl-jstl
Accepted answer
You could just let JSP render the selected
attribute 9 conditionally.
<select class="grade" title="Grade Obtained">
<option value="1" ${qd.grade == '1' ? 'selected' : ''}>1</option>
<option value="2" ${qd.grade == '2' ? 'selected' : ''}>2</option>
<option value="3" ${qd.grade == '3' ? 'selected' : ''}>3</option>
<option value="A" ${qd.grade == 'A' ? 'selected' : ''}>A</option>
<option value="B" ${qd.grade == 'B' ? 'selected' : ''}>B</option>
<option value="C" ${qd.grade == 'C' ? 'selected' : ''}>C</option>
<option value="D" ${qd.grade == 'D' ? 'selected' : ''}>D</option>
<option value="E" ${qd.grade == 'E' ? 'selected' : ''}>E</option>
</select>
Alternatively, you could just 8 create a collection/array of grades and 7 store it in the application scope so that 6 it's available in EL so that you can loop 5 over it using <c:forEach>
. I'm not sure how that would 4 be "clunky". You could use <c:set>
to 3 store them commaseparated and use fn:split()
to split 2 them for <c:forEach>
.
<c:set var="grades" value="1,2,3,A,B,C,D,E" scope="application" />
<select class="grade" title="Grade Obtained">
<c:forEach items="${fn:split(grades, ',')}" var="grade">
<option value="${grade}" ${qd.grade == grade ? 'selected' : ''}>${grade}</option>
</c:forEach>
</select>
This way you end up with more 1 DRY code.
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.