Java 使用JSTL设置HTML下拉选择选项
在同一上下文中,我有另一个查询Java 使用JSTL设置HTML下拉选择选项,java,html,jsp,jstl,el,Java,Html,Jsp,Jstl,El,在同一上下文中,我有另一个查询 <select multiple="multiple" name="prodSKUs"> <c:forEach items="${productSubCategoryList}" var="productSubCategoryList"> <option value="${productSubCategoryList}"${productSubCategoryList == productS
<select multiple="multiple" name="prodSKUs">
<c:forEach items="${productSubCategoryList}" var="productSubCategoryList">
<option value="${productSubCategoryList}"${productSubCategoryList == productSubCategoryName ? 'selected' : ''}>${productSubCategoryList}</option>
</c:forEach>
</select>
${ProductSubCategory列表}
请求中相应的设置如下
for(int i=0;i<userProductData.size();i++){
String productSubCategoryName=userProductData.get(i).getProductSubCategory();
System.out.println(productSubCategoryName);
request.setAttribute("productSubCategoryName",productSubCategoryName);
}
对于(int i=0;i假设您有一个${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>
${selected}
${role}
更新(下一个问题)
您正在覆盖属性“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);
Map Map=newhashmap();
对于Servlet中的(inti=0;i),请执行以下操作:
String selectedRole = "rat"; // Or "cat" or whatever you'd like.
request.setAttribute("selectedRole", selectedRole);
然后在JSP中执行以下操作:
<select name="roleName">
<c:forEach items="${roleNames}" var="role">
<option value="${role}" ${role == selectedRole ? 'selected' : ''}>${role}</option>
</c:forEach>
</select>
除了问题之外:这不是组合框。这是一个下拉列表。组合框是一个可编辑的下拉列表。非常简单。您只需要将字符串“selected”添加到正确的选项中。在下面的代码中,${myBean.foo==val?'selected':'}如果选项的值与bean值相同,将添加字符串“selected”
<select name="foo" id="foo" value="${myBean.foo}">
<option value="">ALL</option>
<c:forEach items="${fooList}" var="val">
<option value="${val}" ${myBean.foo == val ? 'selected' : ' '}><c:out value="${val}" ></c:out></option>
</c:forEach>
</select>
全部的
它将只是无序的。@BalusC:是的,这就是要求的,@sarah:我对你的下一个问题的回答是准备好的。第一个答案是有用的,给我带来了一个想法:如果select带有属性“multiple”他们可能不止一个被选中的角色?@stunaz:我已经在你自己的问题中回答了这个问题:+1,无意中发现了这个问题,想找一个更简洁的selected=“selected”
。非常感谢,我使用了ti,但我发现它应该是“selected=${role==selectedRole?”而不是“selected=${role==selectedRole?”:“}”${role==selectedRole?'selected':''},至少在我的情况下是这样。@NenadBulatovic:有人在中后期编辑了它,我已将它回滚到我的原始答案。
<select name="roleName">
<option value="cat">cat</option>
<option value="rat" selected>rat</option>
<option value="unicorn">unicorn</option>
</select>
<select name="foo" id="foo" value="${myBean.foo}">
<option value="">ALL</option>
<c:forEach items="${fooList}" var="val">
<option value="${val}" ${myBean.foo == val ? 'selected' : ' '}><c:out value="${val}" ></c:out></option>
</c:forEach>
</select>