Reputation: 2579
I am using JSTL
to populate drop down items, where country is an item having { id, name, code }
attributes.
My need is to get name and code for selected country.
for example:
Country{c_id, c_name, c_code}
is structure of country bean. when user selects this item I need to retrieve two values c_name, c_code.
What I did yet:
As I know, only one value can be assigned to itemValue
either c_name
or c_code
.
I tried to populate all country and match selected country and then set to another path variable, but this also not working.
My code is as
<form:select path="selectedCountry" id="ddlCountryId">
<c:forEach items="${countries}" var="country">
<option value="${country.countryName}" ${country.countryName == selectedCountry ? 'selected' : ''}>${country.countryName}</option>
</c:forEach>
</form:select>
<input class="login_submit" type="submit" value="Login" id="btnSubmitId">
<!-- Read country code of selected country -->
<c:forEach var="country" items="${countries}">
<c:out value="country"></c:out>
<c:choose>
<c:when test="${country.countryName==loginCreadetials.selectedCountry}">
<input name="countryCode" type="hidden" value="${country.countryCode}"/>
</c:when>
</c:choose>
</c:forEach>
How can I do this?
Upvotes: 1
Views: 1519
Reputation: 576
Set the value of your option tag to a String that you can easily parse, for instance
value="${country.countryName}:${country.countryName}"
In your controller, you can then split the string on the ':' character to retrieve your two values.
Upvotes: 2