我正在使用 JSTL
填充下拉项,其中国家/地区是具有 { id, name, code }
属性的项目。
我的需要是获取所选国家/地区的名称和代码。
例如:
Country{c_id, c_name, c_code}
是国家 bean 的结构。当用户选择此项目时,我需要检索两个值 c_name、c_code。
我做了什么:
据我所知,只能将一个值分配给 itemValue
,即 c_name
或 c_code
。
我尝试填充所有国家/地区并匹配选定的国家/地区,然后设置为另一个路径变量,但这也不起作用。
我的代码如下
<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>
我该怎么做?
最佳答案
例如,将选项标记的值设置为您可以轻松解析的字符串
value="${country.countryName}:${country.countryName}"
在 Controller 中,您可以在“:”字符上拆分字符串以检索两个值。
关于java - 在下拉选项中设置 bean 属性的值 : Spring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11884115/