[SPR-3389] Nicer handling of Java 5 enums by the Spring MVC form taglib. Created: 18/Apr/07  Updated: 15/Jan/19  Resolved: 22/Jan/09

Status: Closed
Project: Spring Framework
Component/s: Web
Affects Version/s: None
Fix Version/s: 3.0 M2

Type: New Feature Priority: Major
Reporter: William Shields Assignee: Scott Andrews
Resolution: Fixed Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to ROO-254 enums cannot be entered in web interface Closed
Days since last comment: 9 weeks, 6 days ago
Last commented by a User: true
Last updater: Spring Issuemaster

 Description   

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.



 Comments   
Comment by Rick Evans [ 15/Jun/07 ]

Created issue SPR-3594 to track your suggestion of a new <form:radioButtons/> tag.

Comment by Scott Andrews [ 17/Nov/08 ]

Pushing to 3.0 M2

Comment by Erik Lund Jensen [ 17/Nov/08 ]

Spring 2.5 already has support for enums. For example:
[code]
<form:select path="gender">
<form:options items="${genderOptions}" itemValue="code" itemLabel="displayName" />
<form:option value="" label="not specified"/>
</form:select></td>
[/code]

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.
I do not see a need for a PropertyDescriptor. An extension could rather be that itemLabel is used for lookin in i18n property files.

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 ?

Comment by Erik Lund Jensen [ 17/Nov/08 ]

The handling of null can actually be done by adding a property editor:

@InitBinder
public void initBinder(WebDataBinder binder) {
StringNullableEditor stringNullableEditor = new StringNullableEditor(true);
binder.registerCustomEditor(Gender.class, stringNullableEditor);
}

where StringNullableEditor is implemented as:

public class StringNullableEditor extends PropertyEditorSupport {
private Boolean allowEmpty;

public StringNullableEditor(final Boolean allowEmpty)

{ this.allowEmpty = allowEmpty; }

public String getAsText()

{ return (getValue() == null ? "" : (String) getValue()); }

public void setAsText(final String text) throws IllegalArgumentException {
if (this.allowEmpty.equals(Boolean.TRUE) && StringUtils.isEmpty(text))

{ setValue(null); }

else

{ setValue(text); }

}
}

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:
binder.registerCustomEditor(String.class, stringNullableEditor);

Hereby I vote for this case to be closed.

Comment by Erik Lund Jensen [ 17/Nov/08 ]

Just a correction to the sample.
The method getAsText() should be:

public String getAsText() {
return (getValue() == null ? "" : getValue().toString());
}

Comment by Scott Andrews [ 22/Jan/09 ]

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().

Comment by Andy Pemberton [ 12/Aug/09 ]

Scott: was this resolved as part of Spring 3? Or would the fix be available in Spring 2.5.X?

Comment by Spring Issuemaster [ 14/Jan/19 ]

The Spring Framework has migrated to GitHub Issues. This issue corresponds to spring-projects/spring-framework#8072.

Generated at Mon Mar 25 01:54:00 UTC 2019 using JIRA 7.9.2#79002-sha1:3bb15b68ecd99a30eb364c4c1a393359bcad6278.