Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: FAQ: howto display checkboxes/multiselect

  1. #1
    Join Date
    Aug 2004
    Posts
    1,905

    Default FAQ: howto display checkboxes/multiselect

    The question of how to support checkboxes keeps appearing over and over again. This is how I deal with it:

    Assume:

    Code:
      class MyClass {
        private String id;
        private String name;
        // accessors
      }
    
      class MyBackingObject {
        private MyClass[] myClasses;
        // accessors
      }
    In my simpleFormController I register a MyClassPropertyEditor in initBinder:
    Code:
      public initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
        binder.registerCustomEditor(MyClass.class, new MyClassPropertyEditor());
      }
    then in reference data I construct a map of *all* possible instances of MyClass:

    Code:
    public Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception {
      MyBackingObject form = (MyBackingObject) command;
      Set selectedMyClasses = new HashSet().addAll(Arrays.asList(form.getMyClasses()));
    
      Map allMyClasses = new HashMap();
      for (Iterator i = myDAO.getAllMyClasses(); i.hasNext(); ) {
        MyClass myClass = (MyClass) i.next();
        allMyClasses.put(myClass, new Boolean(selectedMyClasses.contains(myClass));
      }
     
      Map model = new HashMap();
      model.put("allMyClasses", allMyClasses);
      return model;
    }
    then in my jsp:

    Code:
      <c&#58;forEach items="$&#123;allMyClasses&#125;" var="allMyClassesEntry">
        <input type="checkbox" name="command.myClasses" value="$&#123;allMyClassesEntry.key.name&#125;" <c&#58;if test="$&#123;allMyClassesEntry.value&#125;">selected</c&#58;if>></input>
      </c&#58;forEach>
    I typed this in, so it probably doesn't compile, but you get the idea

    HTH.

  2. #2

    Default

    You forgot to show the code of MyClassPropertyEditor.

  3. #3
    Join Date
    Sep 2005
    Posts
    13

    Default

    Then how to bind the items?

  4. #4
    Join Date
    Sep 2005
    Posts
    13

    Default

    And another questioin: Dose c:forEach support Map? Can a Map be iteratored by the froEach tag?

  5. #5
    Join Date
    Jul 2005
    Posts
    246

    Default

    Quote Originally Posted by ypj
    And another questioin: Dose c:forEach support Map? Can a Map be iteratored by the froEach tag?
    Yes, you get an Entry object in your var with "key" and "value" values.

    Code:
    <spring&#58;bind path="searchCommand.areaCode">
        <select name="areaCode">
            <option value="">--</option>
            <c&#58;forEach var="county" items="$&#123;countyMap&#125;">
                <option value='<c&#58;out value="$&#123;county.key&#125;"/>' <c&#58;if test="$&#123;status.value == county.key&#125;">selected</c&#58;if>><c&#58;out value="$&#123;county.value&#125;"/></option>
            </c&#58;forEach>
        </select>
    </spring&#58;bind>
    Bob

  6. #6
    Join Date
    Sep 2005
    Posts
    13

    Default

    Thanks Bob.

  7. #7
    Join Date
    May 2005
    Posts
    11

    Default

    also would like to see myclasspropertyeditor...

  8. #8
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    The source code for myClassPropertyEditor is absolutely pointless It entirely depends on what the class is an how to retrieve it.

    For example, if I myClass was a persisted class, i.e. a User and I was entering the user name, then my propertyEditor might look like:

    Code:
      public final UserPropertyEditor extends PropertyEditorSupport &#123;
        private final UserDAO dao;
    
        public UserPropertyEditor &#40;final UserDAO theDAO&#41; &#123;
            this.dao = theDAO;
        &#125;
    
        public String getAsText&#40;&#41; &#123;
            User user = &#40;User&#41; getValue&#40;&#41;;
            return user.getUserName&#40;&#41;;
        &#125;
    
        public void setAsText&#40;final String value&#41; &#123;
            try &#123;
                User user = dao.findUser&#40;value&#41;;
                super.setValue&#40;user&#41;;
            &#125; catch &#40;final ObjectRetrievalFailureException e&#41; &#123;
                LOGGER.debug&#40;"Cannot find user with username&#58; " + value&#41;;
                super.setValue&#40;null&#41;;
            &#125;
        &#125;
      &#125;
    HTH.

  9. #9
    Join Date
    Oct 2005
    Posts
    4

    Default

    Ytesco, I have tried what you have said above and I still can not get my valuies to bind. I have a class that only contains a list, and that list gets populated with my value class that holds all the data. Everything works fine except that the variables on my value class are not getting set with the values from the page when I submit. I have a feeling that it has something to do with the Propert Editor, but I can not figure out how to set that up.

    Do you have any ideas or need more information?

  10. #10
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Can you post your code please.

Similar Threads

  1. Display tree on JSP, recursion?
    By pak in forum Web
    Replies: 3
    Last Post: Jul 9th, 2011, 02:20 AM
  2. How to display message about last action?
    By pir8ped in forum Architecture
    Replies: 8
    Last Post: Jun 6th, 2005, 11:01 AM
  3. Replies: 1
    Last Post: Apr 28th, 2005, 01:54 PM
  4. Replies: 0
    Last Post: Jan 22nd, 2005, 07:28 AM
  5. Replies: 2
    Last Post: Dec 18th, 2004, 10:33 AM

Posting Permissions

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