Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Newbie: Bind Enums to dropdownbox

  1. #1
    Join Date
    Dec 2005
    Posts
    19

    Default Newbie: Bind Enums to dropdownbox

    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.

  2. #2
    Join Date
    Aug 2004
    Location
    The Netherlands
    Posts
    160

    Default

    I take it you mean a java 5 enum?
    Jettro Coenradie
    http://www.gridshore.nl

  3. #3
    Join Date
    Dec 2005
    Posts
    19

    Default

    Yes i mean java 5 enums, i didn't know there were enums in the older versions of java.

  4. #4
    Join Date
    Aug 2004
    Location
    The Netherlands
    Posts
    160

    Wink

    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

  5. #5
    Join Date
    Dec 2005
    Posts
    19

    Default

    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?

  6. #6
    Join Date
    Aug 2004
    Location
    The Netherlands
    Posts
    160

    Default

    Use one of the :
    protected Map referenceData(HttpServletRequest request, Object command, Errors errors, int page) throws Exception {

    methods
    Jettro Coenradie
    http://www.gridshore.nl

  7. #7
    Join Date
    Dec 2005
    Posts
    19

    Default

    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.

  8. #8
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Can you post your code/config please

  9. #9
    Join Date
    Dec 2005
    Posts
    19

    Default

    Code for the enum:
    Code:
    public enum Sex {
    	male,female
    }
    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;
    		}
    	}
    The Form Controller:
    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);
    	}
    Is this the way to do it?

  10. #10
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •