控制器
注意集合是Map<String, List<KeyValueBean>>
.
它存储例如Group 1 ->
{ ("Option 1.1 Label","OPTION_1_1_VAL"), ("Option 1.2 Label","OPTION_1_2_VAL"), ..}
@ModelAttribute("careerOptions")
Map<String, List<KeyValueBean>> getCareerOptions() {
HashMap<String, List<KeyValueBean>> result = new HashMap<String, List<KeyValueBean>>();
result.put("Grp1", new ArrayList<KeyValueBean>());
result.get("Grp1").add(new KeyValueBean("Option 1.1", "OPT_1_1"));
result.get("Grp1").add(new KeyValueBean("Option 1.2", "OPT_1_2"));
result.put("Grp2", new ArrayList<KeyValueBean>());
result.get("Grp2").add(new KeyValueBean("Option 2.1", "OPT_2_1"));
return result;
}
JSP
<form:select path="careerSelected" id="careerElement">
<form:option label="" value="" />
<c:forEach var="optionGroup" items="${careerOptions}">
<optgroup label="${optionGroup.key}">
<c:forEach var="option" items="${optionGroup.value}">
<form:option label="${option.key}" value="${option.value}" />
</c:forEach>
</optgroup>
</c:forEach>
</form:select>
KeyValueBean(我自己创建的,但是 Java 的内置的是AbstractMap.SimpleEntry<K,V>
- 你可以使用那个,只是代码有点丑)
public class KeyValueBean implements Serializable {
private static final long serialVersionUID = 1L;
private String key;
private String value;
public KeyValueBean(String key, String value) {
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
另一种情况:有时 Select 项目是MIXED(Flat 与 OptGroup 混合)
A
B
C (subgroup)
- C.1
- C.2
在这种情况下,Collection 可以是Map<String,Object>
每个条目都将被键入到Object的地方: (1)String或(2)HashMap,我们将从 JSP 中发现。
混合数据结构的控制器
@ModelAttribute("careerOptionsMixed")
Map<String, Object> getCareerOptionsMixed() {
LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>();
result.put("Flat Option 1", "OPT_1_FLAT");
result.put("Group Option 2", myHashMap); // Fill out your HashMap for Group (Key->Value) and add it here
result.put("Flat Option 3", "OPT_3_FLAT");
return result;
}
用于 MIXED 数据结构的 JSP(确定 Collection 与否所需的技巧,通过迭代和设置 var 完成)
在 JSP 中,我们需要确定我们得到的是 Collection 还是 String。唯一的方法是通过迭代和设置一个变量。
<form:select path="career" id="careerField">
<form:option label="" value="" />
<c:forEach var="optionOrOptionGroup" items="${careerOptionsMixed}">
<%-- Must use iteration to find out if this is a Collection or not: https://stackoverflow.com/a/1490171/1005607 --%>
<c:set var="collection" value="false" />
<c:forEach var="potentialOptionGroup" items="${optionOrOptionGroup.value}" varStatus="potentialOptionGroupStatus">
<c:if test="${potentialOptionGroupStatus.index > 0}">
<c:set var="collection" value="true" />
</c:if>
</c:forEach>
<c:choose>
<c:when test="${collection eq true}">
<%-- Now we know this is a LinkedHashMap --%>
<optgroup label="${optionOrOptionGroup.key}">
<c:forEach var="optionGroup" items="${optionOrOptionGroup.value}">
<form:option label="${optionGroup.key}" value="${optionGroup.value}" />
</c:forEach>
</optgroup>
</c:when>
<c:otherwise>
<%-- Now we know this is a flat String --%>
<form:option label="${optionOrOptionGroup.key}" value="${optionOrOptionGroup.value}" />
</c:otherwise>
</c:choose>
</c:forEach>
</form:select>