【问题标题】:Set HTML dropdown selected option using JSTL使用 JSTL 设置 HTML 下拉选择选项
【发布时间】:2011-02-10 12:58:20
【问题描述】:

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

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

}

这里我有多个选择下拉菜单,即使我从 for 获得返回值作为两个,在 UI 中只有一个数据被突出显示,而不是第二个,代码有什么问题?

【问题讨论】:

    标签: java html jsp jstl el


    【解决方案1】:

    假设您有一个集合 ${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>
    

    更新(下一个问题)

    您正在覆盖属性“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="multiple" name="prodSKUs">
        <c:forEach items="${productSubCategoryList}" var="productSubCategoryList">
            <option value="${productSubCategoryList}" ${not empty productSubCategoryMap[productSubCategoryList] ? 'selected' : ''}>${productSubCategoryList}</option>
        </c:forEach>
    </select>
    

    【讨论】:

    • 只会超出排序范围。
    • @BalusC:是的,这就是我们所要求的,@sarah:我已经准备好回答你的下一个问题了
    • 第一个答案很有用,给我带来了一个想法:D
    【解决方案2】:

    在 Servlet 中:

    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>
    

    它将打印 HTML &lt;option&gt; 元素的 selected 属性,因此您最终会像:

    <select name="roleName">
        <option value="cat">cat</option>
        <option value="rat" selected>rat</option>
        <option value="unicorn">unicorn</option>
    </select>
    

    除了问题:这不是组合框。这是一个下拉菜单。组合框是一个可编辑下拉菜单。

    【讨论】:

    • 如果选择具有“多个”属性并且它们可能是多个选定角色怎么办?
    • @stunaz:我已经在你自己的问题中回答了这个问题:stackoverflow.com/questions/4059922/…
    • +1,偶然发现了这个寻找更简洁的&lt;c:if test="${foo = selectedFoo}"&gt;selected="selected"&lt;/c:if&gt;版本。
    • 非常感谢,我使用了 ti,但发现应该只是 ${role = = 选定角色? 'selected' : ''} ,至少在我的情况下。
    • @NenadBulatovic:后来有人对其进行了编辑,我已将其回滚到原来的答案。
    【解决方案3】:

    真的很简单。您只需要将字符串 '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>
    

    【讨论】:

      猜你喜欢
      • 2014-06-28
      • 2010-12-14
      • 1970-01-01
      • 1970-01-01
      • 2014-05-01
      • 2012-03-01
      • 2013-03-17
      • 2021-03-11
      相关资源
      最近更新 更多