sar*_*rah 21 html java jsp jstl el
在相同的上下文中,我有另一个查询
<select multiple="multiple" name="prodSKUs">
<c:forEach items="${productSubCategoryList}" var="productSubCategoryList">
<option value="${productSubCategoryList}"${productSubCategoryList == productSubCategoryName ? 'selected' : ''}>${productSubCategoryList}</option>
</c:forEach>
</select>
Run Code Online (Sandbox Code Playgroud)
并且请求中的相应设置就像
for(int i=0;i<userProductData.size();i++){
String productSubCategoryName=userProductData.get(i).getProductSubCategory();
System.out.println(productSubCategoryName);
request.setAttribute("productSubCategoryName",productSubCategoryName);
}
Run Code Online (Sandbox Code Playgroud)
这里我有多个选择下拉列表,即使我得到两个的返回值,在UI中只有一个数据突然显示而不是第二个,代码中有什么问题?
Mau*_*rry 29
假设你有一个集合$ {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>
Run Code Online (Sandbox Code Playgroud)
更新(下一个问题)
您正在覆盖属性"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);
Run Code Online (Sandbox Code Playgroud)
然后在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>
Run Code Online (Sandbox Code Playgroud)
Bal*_*usC 11
在Servlet中:
String selectedRole = "rat"; // Or "cat" or whatever you'd like.
request.setAttribute("selectedRole", selectedRole);
Run Code Online (Sandbox Code Playgroud)
然后在JSP中执行:
<select name="roleName">
<c:forEach items="${roleNames}" var="role">
<option value="${role}" ${role == selectedRole ? 'selected' : ''}>${role}</option>
</c:forEach>
</select>
Run Code Online (Sandbox Code Playgroud)
它将打印selectedHTML <option>元素的属性,以便您最终:
<select name="roleName">
<option value="cat">cat</option>
<option value="rat" selected>rat</option>
<option value="unicorn">unicorn</option>
</select>
Run Code Online (Sandbox Code Playgroud)
除了问题:这不是一个组合框.这是一个下拉列表.组合框是可编辑的下拉列表.
| 归档时间: |
|
| 查看次数: |
116145 次 |
| 最近记录: |