Java 使用 JSTL 设置 HTML 下拉选择选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2682018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:45:35  来源:igfitidea点击:

Set HTML dropdown selected option using JSTL

javahtmljspjstlel

提问by sarah

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 ?

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

采纳答案by Maurice Perry

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". At the end of the for loop, the last productSubCategoryName.

您正在覆盖属性“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>

回答by BalusC

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 selectedattribute of the HTML <option>element so that you end up like:

它将打印selectedHTML<option>元素的属性,以便您最终像:

<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 nota combo box. This is a dropdown. A combo box is an editabledropdown.

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

回答by MattC

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;

真简单。您只需要将字符串 'selected' 添加到正确的选项中。在下面的代码中, ${myBean.foo == val ? 'selected' : ' '} 将添加字符串 'selected' 如果选项的值与 bean 值相同;

<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>