[英]Set HTML dropdown selected option using JSTL
In the same context i have another query 在相同的上下文中,我有另一个查询
<select multiple="multiple" name="prodSKUs">
<c:forEach items="${productSubCategoryList}" var="productSubCategoryList">
<option value="${productSubCategoryList}"${productSubCategoryList == productSubCategoryName ? 'selected' : ''}>${productSubCategoryList}</option>
</c:forEach>
</select>
and the corresponding setting in request is like 并且请求中的相应设置就像
for(int i=0;i<userProductData.size();i++){
String productSubCategoryName=userProductData.get(i).getProductSubCategory();
System.out.println(productSubCategoryName);
request.setAttribute("productSubCategoryName",productSubCategoryName);
}
here i have multiple select drop down ,even though i get the return value from for as two ,in the UI only one data is getting higlighted not the second one,What is wrong in the code ? 这里我有多个选择下拉列表,即使我得到两个的返回值,在UI中只有一个数据突然显示而不是第二个,代码中有什么问题?
Assuming that you have a collection ${roles} of the elements to put in the combo, and ${selected} the selected element, It would go like this: 假设你有一个集合$ {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>
UPDATE (next question) 更新(下一个问题)
You are overwriting the attribute "productSubCategoryName". 您正在覆盖属性“productSubCategoryName”。 At the end of the for loop, the last productSubCategoryName.
在for循环结束时,最后一个productSubCategoryName。
Because of the limitations of the expression language, I think the best way to deal with this is to use a map: 由于表达式语言的局限性,我认为处理这个问题的最佳方法是使用地图:
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);
And then in the JSP: 然后在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>
In Servlet do: 在Servlet中:
String selectedRole = "rat"; // Or "cat" or whatever you'd like.
request.setAttribute("selectedRole", selectedRole);
Then in JSP do: 然后在JSP中执行:
<select name="roleName">
<c:forEach items="${roleNames}" var="role">
<option value="${role}" ${role == selectedRole ? 'selected' : ''}>${role}</option>
</c:forEach>
</select>
It will print the selected
attribute of the HTML <option>
element so that you end up like: 它将打印HTML
<option>
元素的selected
属性,以便您最终得到:
<select name="roleName">
<option value="cat">cat</option>
<option value="rat" selected>rat</option>
<option value="unicorn">unicorn</option>
</select>
Apart from the problem: this is not a combo box. 除了问题:这不是一个组合框。 This is a dropdown.
这是一个下拉列表。 A combo box is an editable dropdown.
组合框是可编辑的下拉列表。
Real simple. 真的很简单。 You just need to have the string 'selected' added to the right option.
您只需要在正确的选项中添加字符串'selected'即可。 In the following code, ${myBean.foo == val ?
在以下代码中,$ {myBean.foo == val? 'selected' : ' '} will add the string 'selected' if the option's value is the same as the bean value;
'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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.