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 ?
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:
<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". At the end of the for loop, the last 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:
<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:
String selectedRole = "rat"; // Or "cat" or whatever you'd like.
request.setAttribute("selectedRole", selectedRole);
Then in JSP do:
<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:
<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. In the following code, ${myBean.foo == val ? 'selected' : ' '} will add the string 'selected' if the option's value is the same as the bean value;
<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>