Results 1 to 4 of 4

Thread: form:checkboxes unexpected behaviour

  1. #1
    Join Date
    Nov 2006
    Posts
    2

    Default form:checkboxes unexpected behaviour

    Having successfully used SWF 1.x in many of our applications I am now trying to familiarize myself with the changes and new ideas of SWF 2.x. It all looks very promising, but I have encountered an unexpected binding problem related to the form:checkbox tag. I realize that there are many postings on the form:checkbox(es) tag, but none seem to address this particular issue.

    The idea is to have a list of items on my form object that can be deleted using checkboxes. I have designed the form object along the lines of the example in section 14.2.4.4 in the reference documentation:

    Code:
    public class LibraryForm implements Serializable {
    
        List books = new ArrayList();
    
        public LibraryForm() {
            for (int i = 0; i < 10; i++) {
                books.add(new Book(i, "Book number " + i));
            }
        }
        
        public void setBookIdsToDelete(String[] bookIdsToDelete) {
            System.out.println("setBookIds called: " + bookIdsToDelete.length);
            List booksToDelete = new ArrayList();
            for (int i = 0; i < bookIdsToDelete.length; i++) {
                Book bookToDelete = (Book) books.get(Integer.parseInt(bookIdsToDelete[i]));
                booksToDelete.add(bookToDelete);
            }
            books.removeAll(booksToDelete);
        }
    
        public String[] getBookIdsToDelete() {
            return null;
        }
    
        public List getBooks() {
            return books;
        }
    
    
        private class Book implements Serializable {
    
            private int id;
            private String title;
    
            public Book(int id, String title) {
                this.id = id;
                this.title = title;
            }
    
            public int getId() {
                return id;
            }
    
            public void setId(int id) {
                this.id = id;
            }
    
            public String getTitle() {
                return title;
            }
    
            public void setTitle(String title) {
                this.title = title;
            }
        }
    }
    The problem is this. Everything works fine, i.e. binding occurs and elements are deleted as long as 2 or more checkboxes are checked. If only one checkbox is checked, then
    Code:
    public void setBookIdsToDelete(String[] bookIdsToDelete)
    is never invoked. The relevant part of my view page is this
    Code:
    <form:checkboxes path="bookIdsToDelete" items="${booksList}" itemValue="id" itemLabel="title" delimiter="<br/>"/>
    I am using SFW 2.0.2 and Spring 2.5.4.

    Is this a bug or am I missing something? Thanks in advance.
    Last edited by Jesper; Jun 17th, 2008 at 01:49 AM.

  2. #2
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    You're not missing anything. This is a bug, one we are working on for 2.0.3 and should have a fix available soon. In general, the most significant issue remaining in the 2.0.x branch is this issue...

    Keith
    Keith Donald
    Core Spring Development Team

  3. #3
    Join Date
    Nov 2006
    Posts
    2

    Default

    Thanks for the quick reply, Keith. I was pondering this issue for the last few days and didn't really expect it to be a bug but was rather frustrated that I couldn't see what I was missing as my example seemed so simple. I'm looking forward to next release.

  4. #4
    Join Date
    May 2008
    Posts
    11

    Default workaround?

    A temporary workaround that worked for me was to add a hidden parameter with the checkbox name before the iteration of checkboxes is written to your .jsp page. However I did this in a velocity file using the #springBind syntax so not sure if it will work with the Spring 2.0 JSP form tags. The logic then had to dump this id or ignore it in the setBookIdsToDelete method.

    <input type="hidden" name="bookIdsToDelete" value="-1">

    Hope this helps.

Tags for this Thread

Posting Permissions

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