-
Notifications
You must be signed in to change notification settings - Fork 38.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nicer handling of Java 5 enums by the Spring MVC form taglib. [SPR-3389] #8072
Comments
Rick Evans commented Created issue #8276 to track your suggestion of a new form:radioButtons/ tag. |
Scott Andrews commented Pushing to 3.0 M2 |
Erik Lund Jensen commented Spring 2.5 already has support for enums. For example: where genderOptions are the values of the enum Gender. This works fine, except for handling null. If the member (gender) is null then the option value "" should set as selected. William, would itemLabel solve your needs in Spring MVC 2.5? Would it be possible to get the handling of null fixed in Spring MVC 2.5.x ? |
Erik Lund Jensen commented The handling of null can actually be done by adding a property editor:
where StringNullableEditor is implemented as: public class StringNullableEditor extends PropertyEditorSupport { public StringNullableEditor(final Boolean allowEmpty) { public String getAsText() { public void setAsText(final String text) throws IllegalArgumentException { By the way, another comment not really related to this report: The class StringNullableEditor may also be used for handling null values in other input fields. E.g: Hereby I vote for this case to be closed. |
Erik Lund Jensen commented Just a correction to the sample. public String getAsText() { |
Scott Andrews commented The form:options and form:radiobuttons tags will now render a set of options automatically if the bind target is an Enum and items are not otherwise specified. The values of the enum are converted into form inputs where by default the form value is the enum's name() and the form label is the enum's toString(). |
Andy Pemberton commented Scott: was this resolved as part of Spring 3? Or would the fix be available in Spring 2.5.X? |
William Shields opened SPR-3389 and commented
Enums aren't really handled well and could be handled much more nicely by some of the form tags. For example, form:select should be able to autopopulate enum values and would save a lot of boilerplate if this were the case. I'm thinking of a syntax like:
<form:select path="gender">
<form:option value="" label="Select One"/>
form:options/
</form:select>
The empty form:options in this case could, via a PropertyDescriptor, discover the type of the property (being enum Gender) and create the options based on Gender.values() with value of name() and label of toString(). Optional attributes could set the value and label properties eg:
<form:select path="gender" valueProperty="code">
<form:option value="" label="Select One"/>
<form:options labelProperty="description"/>
</form:select>
assuming:
public enum Gender {
MALE("M","Male"),
FEMALE("F","Female");
private String code;
private String description;
Gender(String code, String description) {
this.code = code;
this.description = description;
}
public String getCode() { return code; }
public String getDescription() { return description; }
public String toString() { return description; }
}
Additionally, I would suggest adding a new tag:
form:radioButtons/
to create groups of radio buttons.
Issue Links:
Referenced from: commits 8e261e5
1 votes, 6 watchers
The text was updated successfully, but these errors were encountered: