I am trying to bind an enum to a dropdownbox, and i am having trouble finding some documentation on this.
I probably have to use LabeledEnum. The problem is how all this is tied together.
Can anyone point me to some docs or an example.
I am trying to bind an enum to a dropdownbox, and i am having trouble finding some documentation on this.
I probably have to use LabeledEnum. The problem is how all this is tied together.
Can anyone point me to some docs or an example.
Yes i mean java 5 enums, i didn't know there were enums in the older versions of java.
Me neither, but you could have meant it in a more generic (oops another java 5 term) way. Maybe you have created your own! Well never mind.
Maybe this can help:
An enum has the method values(), this returns an array with all enum values. Then use standard c:foreach library to represent it, something like this:
<select name="${status.expression}">
<option value=""></option>
<c:forEach items="${enumvalues}" var="value">
<option value="${value}">${value}</option>
</c:forEach>
</select>
I know it is not complete, but maybe it gives you a hint.
Jettro Coenradie
http://www.gridshore.nl
To put those values i need to make them available to the view.
I am using simpleFormController, but it dosen't have a method that is called before the request is delivered to the view.
Should i implement a method of one of its superclasses to allow that?
Use one of the :
protected Map referenceData(HttpServletRequest request, Object command, Errors errors, int page) throws Exception {
methods
Jettro Coenradie
http://www.gridshore.nl
Not sure how to use the referenceData function.
Tried to register a enumPropertyEditor. Binding that with
overriding initbinder and registering that one to me enum class.
It was never called.
Can you post your code/config please![]()
Code for the enum:
Code:public enum Sex { male,female }The Form Controller:Code:public class SexPropertyEditor extends PropertyEditorSupport { protected final Log logger = LogFactory.getLog(getClass()); private String female = "female"; private String male = "male"; private Sex type = null; @Override public String getAsText() { logger.debug("getAsText called"); if(type != null){ if(type == Sex.female){ return female; }else { return male; } } return null; } @Override public Object getValue() { logger.debug("getValue called"); return type; } @Override public void setAsText(String arg0) throws IllegalArgumentException { logger.debug("setAsText called"); if(arg0.equalsIgnoreCase(male)){ type = Sex.male; } else if(arg0.equalsIgnoreCase(female)){ type = Sex.female; } else { throw new IllegalArgumentException(); } } @Override public void setValue(Object arg0) { logger.debug("setValue called"); if(((Sex)arg0) == Sex.male ){ type = Sex.male; }else if(((Sex)arg0) == Sex.female){ type = Sex.female; } }
Is this the way to do it?Code:public class UserForm extends SimpleFormController { /** Logger for this class and subclasses * */ protected final Log logger = LogFactory.getLog(getClass()); protected IUserManager userManager; /* (non-Javadoc) * @see org.springframework.web.servlet.mvc.BaseCommandController#initBinder(javax.servlet.http.HttpServletRequest, org.springframework.web.bind.ServletRequestDataBinder) */ @Override protected void initBinder(HttpServletRequest arg0, ServletRequestDataBinder arg1) throws Exception { arg1.registerCustomEditor(Sex.class, new SexPropertyEditor()); super.initBinder(arg0, arg1); }
A couple of points;
- I would call initBinder *before* you do your registration (just a hunch)
- I usually only override getAsText and setAsText
- I would move the toString/fromString behaviour into the enum itself, so your property editor is calling something like Sex.getSex(final String sex) sex.getAsString().
- if the binder is not matching on Sex.class then register it against the name of the property
What does your form look like? I am assuming it has a get/SetSomething which is of type Sex?