Announcement Announcement Module
Collapse
No announcement yet.
Binding Data to <form:radiobuttons> in Spring MVC Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Binding Data to <form:radiobuttons> in Spring MVC

    I have a <form:radiobuttons> component in my jsp page which binds to a collection.

    example
    Code:
    public class Test {
       private Sex sex;
       private String[] sexes = {"male", "female"};
    
       public String[] getSexes() {
            return sexes;
       }
    
       public void setSexes(String[] sexes) {
            this.sexes = sexes;
       }
    
       public Sex getSex() {
            return sex;
       }
    
       public void setSex(Sex sexes) {
            this.sex = sex;
       }
    }
    I bind this data in jsp as below
    Code:
    <form:radiobuttons items="${test.sexes}" path="Sex.description"/>
    My Sex object contain Male and M for description and code. Male is selected.

    This works fine.

    But when I try to use List<Sex> like this.

    Code:
    public class Test {
       private Sex sex;
       private List<Sex> sexes;
    
       public List<Sex> getSexes() {
            return sexes;
       }
    
       public void setSexes(List<Sex> sexes) {
            this.sexes = sexes;
       }
    
       public Sex getSex() {
            return sex;
       }
    
       public void setSex(Sex sexes) {
            this.sex = sex;
       }
    }
    Code:
    <form:radiobuttons items="${test.sexes}" path="Sex.description" itemLabel="description"/>
    Male is not selected. Same thing happens when I use <form:Select>

    Can anyone tell what am I doing wrong?

    Thanks.

    KM

  • #2
    You are binding to the description of Sex, which in this case is null... So there is nothing to bind... You should bind to sex and them create a SexPropertyEditor/Converter which takes the description (or id if it is from a database) and then converts that value to an actual Sex object.

    Also your form data shouldn't be part of the form (it is simply reference data)...

    Comment


    • #3
      Thanks a lot for the response.

      I have never worked with Spring MVC before and needs to look into creating PripertyEditor. Thanks for the heads up.

      Code:
      Also your form data shouldn't be part of the form (it is simply reference data)...
      I do not understand what you mean by the above note. if you can elaborate a little bit would be helpful.

      Thanks again.

      Comment

      Working...
      X