Announcement Announcement Module
Collapse
No announcement yet.
Map<Object,boolean> as input for <checkboxes>? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Map<Object,boolean> as input for <checkboxes>?

    First I thought this must be easy with the <form:checkboxes> tag... But first things first:

    I have several Newsletter-Objects and each user has a Map<Newsletter,boolean> newsletterMap:

    Code:
    petNewsletter --> true
    movieNewsleter --> false
    gardenNewsletter --> true
    ...
    Now I want to implement a SimpleFormController like that:
    Code:
    [x] Pet Newsletter
    [ ] Movie Newsleter
    [x] Garden Newsletter
    The <checkboxes> tag should be bound to an object (FormBackingObject? Simple command object?) that represents the whole list of available Newsletters with checkboxes checked by default, if the corresponding newsletter is in the user's list.

    Any ideas how to achieve that?

  • #2
    You do not need a Mapin the user object to achieve this. Just a List should do.

    view:
    Code:
    <form:checkboxes path="user.newsLetterList" items="${newsLetterRefList}"/>
    In your subclass of SimpleFormController override referenceData
    Code:
    protected Map referenceData(HttpServletRequest request)
                         throws Exception {
    Map referenceData = new HashMap();
    // newsLetterRefList -- complete list of newsletter list
    referenceData.put("newsLetterRefList", newsLetterRefList)
    return referenceData ;
    }
    One last thing you have to do is implement the equals() method in your NewsLetter object, based on which the selected attribute will be populated & also the selected newsletters will be added to the user object on data binding.
    Last edited by maverickthinker; Aug 20th, 2008, 07:48 PM. Reason: Correction

    Comment


    • #3
      OK, thank you. This is my solution:

      I made a custom FormBackingObject as an inner class "userChoice" of my controller. A userChoice has a list of selected Newsletters (just simple Strings).

      Code:
      class UserChoice {
        private List<String> selection = new ArrayList<String>();
        public List<String> getSelection(){
          return selection;
        }
      }
      This method creates the formBackingObject:
      Code:
      protected Object formBackingObject(HttpServletRequest request) throws Exception {
        LOGGER.info("[FBO]");
        UserChoice userChoice = new UserChoice();
        userChoice.getSelection().add("Movie");
        userChoice.getSelection().add("Garden");
        return userChoice;
      }
      This is the referenceData-Method that collects *all possible* newsletters:

      Code:
      protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception {  
        LOGGER.info("[REFERENCE DATA]");
        Map<String,Object> model = new HashMap<String,Object>();
        List<String> newsletters = new ArrayList<String>();
      
        newsletters.add("Pets");
        newsletters.add("Movie");
        newsletters.add("Garden");
        newsletters.add("Art");
      		
        model.put("newsletters", newsletters);
        return model;    
      }
      My checkboxes are bound to the userChoice's selection:

      Code:
      <spring-form:form>
        <spring-form:checkboxes path="selection" items="${newsletters}" />
        <input type="submit" name="submit" value="Submit" />
      </spring-form:form>
      The boxes corresponding to the user's selection are preselected. That's good. Unfortunately I don't get the user's changes:

      Code:
      protected void doSubmitAction(Object command) throws Exception {
        LOGGER.info("[DO SUBMIT ACTION]");
        UserChoice userChoice = (UserChoice)command;
        userChoice.logSelection();
      }
      logSelection still shows Movie and Garden, even when I select only Pets and Art. Any idea why? BTW: Where do I compose the model for the success view? Do I have to use onSubmit instead of doSubmitAction?

      Comment

      Working...
      X