假设您有$ {roles}个要放入组合的元素,并且$ {selected}个选择的元素,它将像这样:
<select name='role'> <option value="${selected}" selected>${selected}</option> <c:forEach items="${roles}" var="role"> <c:if test="${role != selected}"> <option value="${role}">${role}</option> </c:if> </c:forEach></select>更新(下一个问题)
您正在覆盖属性“ productSubCategoryName”。在for循环的末尾,最后一个productSubCategoryName。
由于表达语言的局限性,我认为处理此问题的最佳方法是使用地图:
Map<String,Boolean> map = new HashMap<String,Boolean>();for(int i=0;i<userProductData.size();i++){ String productSubCategoryName=userProductData.get(i).getProductSubCategory(); System.out.println(productSubCategoryName); map.put(productSubCategoryName, true);}request.setAttribute("productSubCategoryMap", map);然后在JSP中:
<select multiple="multiple" name="prodSKUs"> <c:forEach items="${productSubCategoryList}" var="productSubCategoryList"> <option value="${productSubCategoryList}" ${not empty productSubCategoryMap[productSubCategoryList] ? 'selected' : ''}>${productSubCategoryList}</option> </c:forEach></select>


