Instead of hardcoding the years in my dropdownlist, I’m going to pass over a year as the first option value and then want the following 2 values to be the previous year and so on. Something like this:
<select name="workloadYear">
<option value="2008" <c:if test="${form.workloadYear == 2008}">selected="selected"</c:if>>2008</option>
<option value="2007" <c:if test="${form.workloadYear == 2007}">selected="selected"</c:if>>2007</option>
<option value="2006" <c:if test="${form.workloadYear == 2006}">selected="selected"</c:if>>2006</option>
</select>
How can I do this without hardcoding the years?
You can get the current year in EL as follows:
(it’s now available as
${year})You can do a loop in JSP using
<c:forEach>:This will display the current year and the 2 previous years as options.