Java 如何用 List<String> 填充 <form:select>?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9809263/
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-16 07:56:02  来源:igfitidea点击:

How to populate <form:select> with List<String>?

javaspringjsp

提问by Dragan

I have a List<String>in the controller which im passing to the view. I need to populate <form:select>with that data.

List<String>在控制器中有一个我传递给视图。我需要填充<form:select>这些数据。

I tried setting the itemValueattribute to "name"but that did not work.

我尝试将itemValue属性设置为,"name"但这不起作用。

采纳答案by bsimic

You can do the following:

您可以执行以下操作:

<form:select path="selectName">
    <form:option value="0" label="Select an Option" />
    <form:options items="${nameOfList}" />
</form:select>

By providing only the items attribute to the form:options tag, it should make the value and label the value of each String in your list.

通过仅向 form:options 标签提供 items 属性,它应该为列表中的每个字符串的值设置值和标签。

回答by Rashedul.Rubel

you can also try as follows:

你也可以尝试如下:

<form:select path="country">
<form:option value="NONE" label="--- Select ---" />
<form:options items="${countryList}" itemValue="value" itemLabel="description"/>
</form:select>

回答by José Mendes

protected Map referenceData(HttpServletRequest request) throws Exception {
Map referenceData = new HashMap();
Map<String,String> country = new LinkedHashMap<String,String>();
country.put("US", "United Stated");
country.put("CHINA", "China");
country.put("SG", "Singapore");
country.put("MY", "Malaysia");
referenceData.put("countryList", country);

}

}

Then

然后

<form:select path="country" items="${countryList}" />