Results 1 to 3 of 3

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

  1. #1
    Join Date
    Feb 2008
    Posts
    17

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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)...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Feb 2008
    Posts
    17

    Default

    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.

Posting Permissions

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