Announcement Announcement Module
No announcement yet.
Problem binding ManyToMany to Set Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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:


    public class User implements {
        private int id;
        private String name;
        private Set<Role> role = new HashSet<Role>(0);
    and Role:

    public class Role implements {
        private int id;
        private String name;
    i've defined a flow to insert a user: createuser.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns=""
           <persistence-context />
               <evaluate expression="userManager.createUser()" result="flowScope.user" />
           <view-state id="create_user" model="user" >
               <binding property="name" />           
               <binding property="role" converter="roleToSet"/>
                <evaluate expression="roleManager.allRoles()" result="viewScope.rolesList" />
    and the converter roleToSet:

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

    <form:select path="role">
         <form:options items="${rolesList}" itemLabel="name"/>
    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:

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

    <c:forEach var="error" items="${flowRequestContext.messageContext.allMessages}">
    So, when i call my view, the console print the output of convertSourceToTargetClass:

    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


    • #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.