Results 1 to 10 of 10

Thread: CustomCollectionEditor and form:options

  1. #1
    Join Date
    Oct 2006
    Posts
    23

    Default CustomCollectionEditor and form:options

    Hate to bring this up but after a day of failing to get this to work I could use some help.

    I have an "edit user" page where you edit some user properties and assign roles to the user. There is nothing fancy about my user class:

    Code:
    public User {
      private String username;
      private Set<Role> roles;
      // get/set stuff
      ...
    }
    In my JSP I'm using the Spring form tags:

    Code:
    <form:select path="roles">
      <c:forEach items="${allRoles}" var="role">
        <form:option value="${role.id}"><fmt:message key="option.role.${role.name}"/></form:option>
      </c:forEach>
    </form:select>
    And in my controller I wrote a CustomCollectionEditor as suggested all over the place:

    Code:
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
    {	
      binder.registerCustomEditor(Set.class, "roles", new CustomCollectionEditor(Set.class)
      {
        protected Object convertElement(Object element)
        {
          Long roleId = null;
    				
          if (element instanceof Long)
            roleId = (Long) element;
          else if (element instanceof String)
            roleId = Long.valueOf((String) element);
    				
          return roleId != null ? roleFactory.read(roleId) : null;
        }
      });
    }
    I've stepped through the code and everything seems to be working right, the convertElement() method is doing what I think it's supposed to do, but when the form is displayed the option values are empty!

    Code:
    <select id="roles" name="roles" multiple="multiple">
      <option value="">Superuser</option>
      <option value="">Normal User</option>
      <option value="">Some Other User</option>
    </select><input type="hidden" name="_roles" value="1"/>
    If I write the options myself then ${role.id} outputs the right value.

    Can anyone see what I'm doing wrong? I've tried everything I can think of. The following forum post and open bug report (From 2006!) are similar and make me wonder if this is still really an open issue?

    http://opensource.atlassian.com/proj...rowse/SPR-2633
    http://forum.springframework.org/showthread.php?t=29849
    Last edited by tduffey; Feb 19th, 2007 at 11:10 AM. Reason: Added links to relevant posts

  2. #2
    Join Date
    Feb 2007
    Posts
    3

    Default

    When I tried this, I was able to get one of the options that I selected. After reading the API docs for ServletRequestDataBinder.registerCustomEditor, it appears that it wants to know what the incoming type is. Which, in our case, would be String[]. The error message I got said that String[] was not a collection type.
    So, as good as this looks in theory, I don't think that all of the kinks are worked out yet.
    It appears that to get the multiple selections working, you can't bind the field to spring or it will cry that you haven't correctly converted the types. Which means that you have to handle the incoming selections via a String[] in the onBind() method in the form controller.
    Code:
        protected void onBind(HttpServletRequest request, Object command, BindException errors)
        {
            ArwUser user = (ArwUser) command;
            String[] selectedApplications = request.getParameterValues("selectedApplications");
            if (selectedApplications != null)
            {
                user.setApplications(new ArrayList(Arrays.asList(selectedApplications)));
            }
        }
    A couple of notes:
    1. I can't get data bindings to work with multiple selects. After 3 days of pounding on it and searching google and the Spring forums I ended up with nothing.
    2. I found it best to use a separate property in my user class that represents the selected options. Then, in the onBind() method repopulate the list in my user object with the values that were posted. This seems to be the best way.
    3. The Spring form tags are great, but I think that they are either lacking in this area or I'm just not using it correctly. Either way, the documentation on some of this stuff...well...it just sucks. I'm really disappointed in the extreme lack of good documentation in Spring 2.0. It seems to me that they are sending the message "go look in the api and in the sample apps first". The manual is just terrible. I'll wait to see how the Manning "Spring In Action" book fares. But, what we have to go on so far is pretty dismal.

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    As I stated in the other thread already the CustomCollectionEditor is to create Collections (List, Set, ?). So it will populate the desired collection with elements of the desired type.

    However it is not intended to convert single elements into a value. It is designed to work on Collections, not on a single Role instance. You want 1 PropertyEditor to do 2 tasks for you.

    [EDIT]

    I just created a working solution. If you're interested I can send you a war file with some examples. I'll try to get them deployed somewhere on the internet, but yesterday I couldn't get my war uploaded. A war with source code can be downloaded here. Now lets see if I can get some free/cheap java hosting to get this war deployed on the internet.
    Last edited by Marten Deinum; Feb 22nd, 2007 at 06:17 AM.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #4
    Join Date
    Mar 2007
    Posts
    11

    Default Access problems?

    Quote Originally Posted by mdeinum View Post
    I just created a working solution. If you're interested I can send you a war file with some examples. I'll try to get them deployed somewhere on the internet, but yesterday I couldn't get my war uploaded. A war with source code can be downloaded here.
    I'd love to see that war file - but - the deinum.biz domain is just giving me a 403 forbidden error.
    Last edited by chrissearle; Mar 13th, 2007 at 04:52 AM.

  5. #5
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Yeah, I'm currently moving my domain from one provider to another. Will give a hint when everything is back online.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  6. #6
    Join Date
    Mar 2007
    Posts
    11

    Default

    Could I be cheeky and ask you to mail me a copy of the war file? This is the last thing I need to get working and I've been hacking at it for several days now. If so - you should be able to grab my vcard from my profile

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    I could if I had the war at hand, but I do not. Will check this evening to see if I can post it here of email it to you.

    Basically it comes down to create a PropertyEditor which can handle elements as well as collections.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  8. #8
    Join Date
    Mar 2007
    Posts
    11

    Default

    OK - many thanks.

  9. #9
    Join Date
    Feb 2007
    Posts
    3

    Default

    So...a COLLECTION of String objects is out of the question?

  10. #10
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Tom, you added a reference to SPR-2633 and a reference to this thread there, but got no direct response. I now had the same issue from what I understand with form:select bound to a collection. Since I'm quite sure that there IS something broken I also added an issue (SPR-3654) to Jira.

    Jörg

Posting Permissions

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