Announcement Announcement Module
Collapse
No announcement yet.
Problem binding ManyToMany to Set Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem binding ManyToMany to Set

    Hi people, I'm facing a problem 'cause i cant bind a list with a select multiple html element.
    The problem is on the submit of the form, if I select multiple elements from my html form, nothing is submited and my view is rendered another time.

    I've created a custom converter: roleToSet to convert strings to set, without success:

    I've defined two model class with setters and getters:

    User:

    Code:
    @Entity
    public class User implements java.io.Serializable {
    
        @Id
        @GeneratedValue
        private int id;
        private String name;
        @ManyToMany(cascade=CascadeType.PERSIST)
        private Set<Role> role = new HashSet<Role>(0);
    and Role:

    Code:
    @Entity
    public class Role implements java.io.Serializable {
    
        @Id
        private int id;
        private String name;
    i've defined a flow to insert a user: createuser.xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns="http://www.springframework.org/schema/webflow"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.springframework.org/schema/webflow http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
    
           <persistence-context />
    
           <on-start>
               <evaluate expression="userManager.createUser()" result="flowScope.user" />
           </on-start>
    
           <view-state id="create_user" model="user" >
           <binder>
               <binding property="name" />           
               <binding property="role" converter="roleToSet"/>
           </binder>
            <on-render>
                <evaluate expression="roleManager.allRoles()" result="viewScope.rolesList" />
            </on-render>
    
    </flow>
    and the converter roleToSet:

    Code:
    public class roleToSet extends StringToObject {    
    
        public roleToSet() {
            super(Set.class);
        }   
    
        @Override
        protected Object toObject(String string, Class targetClass) throws Exception {
            ....        
        }
    
        @Override
        protected String toString(Object object) throws Exception {        
            Role role = (Role) object;
            return role.getName();
        }        
    }
    the problem is on my SELECT element:

    Code:
    <form:select path="role">
         <form:options items="${rolesList}" itemLabel="name"/>
    </form:select>
    If I select only one element and then i submit the form, the toObject method is called and the object will update to the database correctly.
    If i select multiple elements, it doesn't submit the form, but redirects me the formView instead.

    I need some help, i can't figre the problem 'cause no exception is raised

  • #2
    Ok, some update news:

    I've started to debug and I implemented roleToSet directly from TwoWayConverter:

    Code:
    public class RoleToSet implements TwoWayConverter {
    
        public Object convertTargetToSourceClass(Object target, Class sourceClass) throws Exception {
            System.out.println(target);
            return target;
        }
    
        public Class getSourceClass() {
            return Object.class;
    
        }
    
        public Class getTargetClass() {
            return Object.class;
        }
    
        public Object convertSourceToTargetClass(Object source, Class targetClass) throws Exception {
            System.out.println(source);
            return source;
        }  
    }
    in my form view I put this string to know the error type:

    Code:
    <c:forEach var="error" items="${flowRequestContext.messageContext.allMessages}">
            <p>${error.text}</p>
    </c:forEach>
    So, when i call my view, the console print the output of convertSourceToTargetClass:
    Role@1e1ee5d
    Role@17e8c5b
    Role@17e8c5b

    and then if I select and submit only one role, everything went smoothly and i receive the output from convertTargetToSourceClass.
    if I select two or more options from my select, flowRequestContext.messageContext.allMessages give me: typeMismatch on role and no output from convertTargetToSourceClass

    Comment


    • #3
      If you really want help you should post the contents of the 'source' parameter when your convertSourceToTargetClass method is called for multiple selections. And probably the body of the toObject method in your first post too.

      I don't really know how it works, but a multiple selection most probably will return a String with comma separated values.
      So you could try to convert the string splitting it and creating a Set<Role> with the values.

      I'm not sure, just a thought.

      Btw, while you are debugging (and probably for production too) I wouldn't use a converter, but will have the methods void setRole(String list) and String getRole() in your form model class and will parse and convert the strings in them. That way you will avoid the typeMismatch error and some others too.

      Good luck, and please leave a note when you fix it.
      Last edited by triqui; Feb 15th, 2010, 12:12 PM.

      Comment

      Working...
      X