Announcement Announcement Module
No announcement yet.
Spring pre-select/pre-populate MULTI-SELECT box OPTIONs Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring pre-select/pre-populate MULTI-SELECT box OPTIONs

    I have a Book class which has properties such as id (long), name (String). Also, one of its properties is

    Author has properties such as id, name, biography.

    I have a book search form in which the total set of the Authors in my DB appears in a multi-select box. I was able have the authors that were selected by the user sent correctly to the controller (i.e. I could get them through the command as Set of Authors), but I want the next page (i.e. the result view page, which is the same page as the first search page) also to show the original search form filled with the values that were selected by the user. This was easy with regular text fields, e.g. the book's name: in the controller I got them loaded on the command object, and I simply kept them unchanged before sending the same command object to the view. But keeping the command's Set of Authors did not cause the view page to pre-select the relevant Authors in the multi-select box.... HOW DO I DO THIS TRICK???

    I have read in the following link

    Spring MVC JSTL preselected list

    that I need to "to override the equals method of the" Author class.... I did it (two Author classes are equal if they have the same id), but it did not help. Still, no Author is selected in the multi-select box in result page after I submit the search form.


  • #2
    I would do it like this with Spring's forms taglib:

    <form:select path="authors">
       <form:option value="-" label="--Please Select"/>
       <form:options items="${allAuthors}" itemValue="id" itemLabel="name"/>
    Here "authors" is a bound property of command object that contains only the selected authors. ${allAuthors} is a model object that contains all the available authors.


    • #3
      Hi. I already have something like this in my code. But it does not cause the selected authors (i.e. "authors") to appear as selected in the result view. Other properties of the command, such as those with path that matches a text field, do show the value previously entered by the user. But the "authors" does not: none of the options is highlighted/selected.
      Have you tried it to see that it actually works?

      What about the link I have mentioned? They say there that something else should be done (which I tried but it did not help).


      • #4
        Sorry, that was my JSF-infected mindset talking.

        The code I gave you will not work for obvious reasons. Spring MVC, unlike component frameworks like JSF, does not restore component tree from session on every postback. Which means that even though you put List<Author> to your model, what comes back in HTTP request is just a list of selected IDs in HTTP POST body, something like "authors=2&authors=5&authors=12". Thus, Spring MVC has no way of restoring the original List<Authors>.

        What you should do instead is have a List<Long> selectedAuthorIds in your search form object, which Spring will have no trouble binding. I just tested that it works with a select field like this:

        <form:select multiple="true" size="5" path="selectedAuthorIds" items="${allAuthors}" itemValue="id" itemLabel="name"/>