使用JSTL设置HTML下拉选择选项 [英] Set HTML dropdown selected option using JSTL

查看:132
本文介绍了使用JSTL设置HTML下拉选择选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在同样的上下文中,我有另一个查询

 < select multiple =multiplename =prodSKUs> 
< c:forEach items =$ {productSubCategoryList}var =productSubCategoryList>
< option value =$ {productSubCategoryList}$ {productSubCategoryList == productSubCategoryName? 'selected':''}> $ {productSubCategoryList}< / option>
< / c:forEach>
< / select>

以及请求中的相应设置就像

  for(int i = 0; i< userProductData.size(); i ++){
String productSubCategoryName = userProductData.get(i).getProductSubCategory();
System.out.println(productSubCategoryName);
request.setAttribute(productSubCategoryName,productSubCategoryName);




$ b $ p
$ b

这里我有多个选择下拉菜单,即使我得到来自for的返回值为两个,在UI中只有一个数据会变得更亮,而不是第二个,代码中出现了什么错误?

解决方案

假设您有一个要放入组合的元素的集合$ {roles},并且选择了元素$ {selected},它将如下所示:

 < select name ='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);

然后在JSP中:

 < select multiple =multiplename =prodSKUs> 
< c:forEach items =$ {productSubCategoryList}var =productSubCategoryList>
< option value =$ {productSubCategoryList}$ {not empty productSubCategoryMap [productSubCategoryList]? 'selected':''}> $ {productSubCategoryList}< / option>
< / c:forEach>
< / select>


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>

这篇关于使用JSTL设置HTML下拉选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆