Managing the Data
In this scenario, we are dealing with a collection named
Map<String, List<KeyValueBean>>
. This collection holds information like:
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;
}
Implementing in 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>
Custom Java Bean
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;
}
}
Dealing with Mixed Data Structures
Sometimes, the Select items have a mix of flat and grouped options as shown below:
A
B
C (subgroup)
- C.1
- C.2
In such cases, the Collection can be a Map where each entry is tied to an Object: (1) a String or (2) a HashMap. The distinction between them will be made in the JSP.
Controller for a Mix of Data Structures
@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;
}
JSP for Dealing with Mixed Data Structures
<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>