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

Closed
spring-issuemaster opened this Issue Apr 18, 2007 · 7 comments

Comments

Projects
None yet
1 participant
@spring-issuemaster
Copy link
Collaborator

spring-issuemaster commented Apr 18, 2007

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:

  • ROO-254 enums cannot be entered in web interface

Referenced from: commits 8e261e5

1 votes, 6 watchers

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator

spring-issuemaster commented Jun 15, 2007

Rick Evans commented

Created issue #8276 to track your suggestion of a new form:radioButtons/ tag.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator

spring-issuemaster commented Nov 17, 2008

Scott Andrews commented

Pushing to 3.0 M2

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator

spring-issuemaster commented Nov 17, 2008

Erik Lund Jensen commented

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 ?

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator

spring-issuemaster commented Nov 17, 2008

Erik Lund Jensen commented

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.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator

spring-issuemaster commented Nov 17, 2008

Erik Lund Jensen commented

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

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

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator

spring-issuemaster commented Jan 22, 2009

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

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator

spring-issuemaster commented Aug 12, 2009

Andy Pemberton commented

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment