Results 1 to 8 of 8

Thread: form:radiobutton and enum

  1. #1
    Join Date
    Feb 2005
    Posts
    12

    Default form:radiobutton and enum

    I have a command class with a property that is an enum type, I'm trying to present the property on a JSP form as a set of radio buttons:

    Enum:
    Code:
    public enum PurchaseRate {
        Bid, Offer
    }
    Command:
    Code:
    ...
    private PurchaseRate purchaseRate;
    ...
    JSP:
    Code:
    Bid: <form:radiobutton path="purchaseRate" value="Bid"/> <br/>
    Offer: <form:radiobutton path="purchaseRate" value="Offer"/> <br/>
    No matter what the value of the purchaseRate property, no radio button is ever selected.

  2. #2
    Join Date
    Apr 2008
    Posts
    1

    Default

    Has anyone found a solution to this problem?

    Im wanting to do the same thing.

    Perhaps you can use an enum as the value?
    E.g.

    Code:
    <form:radiobutton path="purchaseRate" value="PurchaseRate.Offer"/>
    I would really be happy to find a solution to this problem.

    Maybe someone could explain how to bind enum types to jsp?

    Thanks

  3. #3
    Join Date
    May 2006
    Location
    Santa Maria, RS, Brasil
    Posts
    15

    Default radio button enum

    My Enum:
    Code:
    public class enum MyEnum {
       ABC("XYZ"),
       DEF ("KW");
       private String description;
       public MyEnum(description) { this.description = description;}
       public String getDescription() { return description;}
    }
    Command:
    Code:
    public class Command {
     private MyEnum myPath;
     // getters and setters
    }
    Controller.referenceData():
    Code:
     map.put("enums", MyEnum.values());
    JSP:
    Code:
    <c:forEach var="enum" items="${enums}">
     <form:radiobutton path="myPath" value="${enum}"/>${enum.description}
    </c:forEach>
    Generated HTML:
    Code:
     <input type="radio" name="myPath" value="ABC">XYZ
     <input type="radio" name="myPath" value="DEF">KW
    You do NOT need register a Custom Editor ! The conversion occurs automagically

    When you post the form, the spring will look for a enum with the name of "myPath" parameter value and configure the object in the command.

  4. #4
    Join Date
    Feb 2005
    Posts
    12

    Default

    Yes, thanks for the reply, but I think you missed the point of the original posting - none of the radio buttons are selected when the command class already contains a value for the enumeration property!

  5. #5
    Join Date
    May 2006
    Location
    Santa Maria, RS, Brasil
    Posts
    15

    Default Works

    For me, works.

    Controller:
    Code:
     formBackingObject(...) {
      final Command cmd = new Command();
      cmd.setMyPath(MyEnum.ABC);
      return cmd;
    }
    HTML:
    Code:
    <input type="radio" name="myPath" value="ABC" checked>XYZ
    <input type="radio" name="myPath" value="DEF">KW
    Remember that the radio value must have the same Enum name.

  6. #6
    Join Date
    Feb 2005
    Posts
    12

    Default

    Thanks for the reply, but just hard-coding a 'checked' attribute in the HTML is no good when editing an entity that may have any of the enumeration's values.

  7. #7
    Join Date
    May 2006
    Location
    Santa Maria, RS, Brasil
    Posts
    15

    Default Works

    CowinR, I did not make the manual check the radio button.
    The html posted is the generated when the page called. I just make this example for show to you that works !

    Here, for my applications, we use enums and radio buttons and works perfectly.

  8. #8
    Join Date
    May 2008
    Posts
    3

    Default

    Naming the values exactly as the ENUM names worked perfectly for me.

    Thanks for the tip!

Posting Permissions

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