I have form-backing object:
public class MyDto {
private Set<MyEnum> myEnum = new HashSet<MyEnum>();
// getters/setters
public MyEnym[] getMyEnumValues() {
return MyEnum.values();
}
}
public MyEnum {
A, B, C
}
What is the way to show all enum values in <form:select multiple="true"/>
and achieve automatically mapping of selected values to myEnum
field in my form-backing object?
Update: Some code:
<form:select path="myEnum" multiple="true" items="${myDto.myEnumValues}"/>
When submitting form, selected values in multiselect are presented in HTTP request:
myEnum: A
myEnum: B
public String saveMyDto(@Valid @ModelAttribute("myDto") MyDto myDto) {
log.debug("Enum list: " + myDto.myEnum().toString());
....
}
The key idea is to assign the values to the
item
attribute ofform:select
.I the controller that populate the view with the form add
in the jsp use:
(On the other hand, I remember that I have had a look at the select tag implementation of spring, and found that it the value of the actual value is an Enum, then spring automatically use all
Enum.values
as default value foritems
(but I am not 100% sure) )The Controller method should look like