Announcement Announcement Module
Collapse
No announcement yet.
Bind <checkboxes> to List<NoString> Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Bind <checkboxes> to List<NoString>

    When I bind my <form:checkboxes> to a list of Strings everything is fine:

    Code:
    <spring-form:checkboxes path="selectedStrings" items="${allStrings}" />
    When I bind them to a list of Complex Objects I get a Data Binding error:

    Code:
    <spring-form:checkboxes path="selectedObjects" items="${allObjects}" />
    Do I have to use the attribute "itemValues"? But what to use? I thought the values are the objects itself.

  • #2
    I could be wrong (I'm not a Spring guru) but if you wish to bind to a complex object you will need to create a Property Editor to Stringify/Beanify your object.

    Comment


    • #3
      From the manual:
      You can also use a custom object where you can
      provide the property names for the value using "itemValue" and the label using "itemLabel".
      So, for your complex object, you need to specify the label and value, so that Spring knows how to turn in into html.

      xcs is right, you'll need to use write a property editor, *if* you want the value to be the object itself. Spring has built in property editors for primitive types like numbers and strings, so you could use some of the primitives that are fields in your complex object. For instance, itemValue="myBean.id", itemLabel="myBean.name".

      Comment


      • #4
        [Deleted a complex example]
        Last edited by TSH; Aug 25th, 2008, 10:28 AM. Reason: Too complex

        Comment


        • #5
          I changed my example and hope it is now easier to understand. A user can subscribe to 0-n newsletters.

          Summary:
          I have a List<Newsletter> as the referenceData. I have a List<Newsletter> as a property of my FormBackingObject. The boxes corresponding to the Newsletters which are in the FormBackingObjects lists are checked. Fine! When I click on some boxes, a Data Binding Error occurs. I hoped the FormBackingObject's old List<Newsletter> would be replaced by a list corresponding to the selected boxes.

          Code:
          public interface Newsletter {
            public String getLabel();
            public String getId();
          }
          
          public class PetNewsletter implements Newsletter {
            ...
          }
          
          public class GardenNewsletter implements Newsletter {
            ...
          }
          My FormBackingObject is an inner class called UserChoice:

          Code:
          public class UserChoice {
            List<Newsletter> selectedNewsletters = new ArrayList<Newsletter>();
          
            public UserChoice(User user) {
              List<Newsletter> usersSelection = this.getNewsletters(user);
              this.setSelectedNewsletters(usersSelection);
            }
          	
            public List<Newsletter> getSelectedNewsletters() {
              return selecteNewsletters;
            }
          
            public void setSelectedNewsletters(List<Newsletter> selectedNewsletters) {
              this.selectedNewsletters = selectedNewsletters;
            }
          	
          }
          
          protected Object formBackingObject(HttpServletRequest request) throws Exception {
            User user = userManager.getUser();
            return new UserChoice(user);
          }
          The referenceData is a list of all available Newsletters:

          Code:
          protected Map<String,Object> referenceData(HttpServletRequest request) throws Exception {
          
            List<Newsletter> allNewsletters = newsletterManager.getAllNewsletters();
            model.put("allNewsletters", allNewsletters);
            return model;
          }
          Inside the JSP I create a list of checkboxes:
          Code:
          <spring-form:form>
            <spring-form:checkboxes path="selectedNewsletters" items="${allNewsletters}" />	
            <input type="submit" name="submit" value="Submit" />
          </spring-form:form>
          The form seems to work: If the FormBackingObject contains 3 Newsletters, the corresponding checkboxes are checked. The problem is, that my selection can not be written back to the bound UserChoice object. If I select more than 0 checkboxes a "Data binding errors: 1" occurs and I get redirected to the form. I also tried the attributes

          Code:
          itemLabel="label" (works fine)
          itemValue="id" (the HTML is now value="petletter" instead of "...PetNewsletter@12394", but no further effect)

          Comment


          • #6
            Code:
            List<Newsletter> allNewsletters = newsletterManager.getAllNewsletters();
            Are you getting those newsletters from a database? Whenever I work with a form in which the user selects from a list of variables from a DB (in this case, newsletters), I use only the primary key on the formBackingObject. So, you might consider replacing List<Newsletter> with List<Integer>, assuming your keys are integers.

            On the form, the user will make their selection(s), the values of which are mapped to newsletter.id. The list of integers is easily populated from this.

            Then, when you need to actually get a newsletter object, you can call something like:
            Code:
            newsletterManager.getNewsletterById()

            Comment


            • #7
              Thank you! So my mistake was to compare the whole Newsletter objects instead of just one plain value like ID? I think this will work!

              Comment


              • #8
                Just a quick note, and you probably caught this, but that code I posted should have had an argument:
                Code:
                newsletterManager.getNewsletterById(int id)
                Early morning mistakes

                Comment

                Working...
                X