Announcement Announcement Module
Collapse
No announcement yet.
form:radiobutton and enum Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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

    Comment


    • #3
      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.

      Comment


      • #4
        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!

        Comment


        • #5
          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.

          Comment


          • #6
            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.

            Comment


            • #7
              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.

              Comment


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

                Thanks for the tip!

                Comment

                Working...
                X