0

I have stayed on this issue for sometimes now. Can someone tell me what I am getting wrong

Groupp.java

public class Groupp implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @Basic(optional = false)
    @NotNull
    private String name;
    @Basic(optional = false)
    @NotNull
    private String description;
    @JoinTable(name = "group_role", joinColumns = {
        @JoinColumn(name = "group_id", referencedColumnName = "id", nullable = false)}, inverseJoinColumns = {
        @JoinColumn(name = "role_id", referencedColumnName = "id", nullable = false)})
    @ManyToMany
    private List<Role> roles;
    //getters and setters
}

GroupController.java

@Named("groupController")
@SessionScoped
public class GroupController implements Serializable {

    private Groupp current;
    @EJB
    private GroupFacade ejbFacade;

    public GroupController() {
    }

    public Groupp getSelected() {
        if (current == null) {
            current = new Groupp();
            selectedItemIndex = -1;
        }
        return current;
    }

    public String createGroup() {
        System.out.println("********Start Create Group***********");
        try {
            getFacade.create(current);

            return null;
    }


@FacesConverter(forClass = Groupp.class)
public static class GrouppControllerConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
        System.out.println("Groupp::Submitted: String:  "+value);
        if (value == null || value.length() == 0) {
            return null;
        }
        GroupController controller = (GroupController) facesContext.getApplication().getELResolver().
                getValue(facesContext.getELContext(), null, "grouppController");
        System.out.println("Groupp::Object of Submitted: String:  "+controller.getGroupp(getKey(value)));
        return controller.getGroupp(getKey(value));
    }

    java.lang.Integer getKey(String value) {
        java.lang.Integer key;
        key = Integer.valueOf(value);
        return key;
    }

    String getStringKey(java.lang.Integer value) {
        StringBuilder sb = new StringBuilder();
        sb.append(value);
        return sb.toString();
    }

    @Override
    public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
        if (object == null) {
            return null;
        }
        if (object instanceof Groupp) {
            Groupp o = (Groupp) object;
            return getStringKey(o.getId());
        } else {
            throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + Groupp.class.getName());
        }
    }

}

}

RoleController.java

@Named("roleController")
@SessionScoped
public class RoleController implements Serializable {

    private Role current;
    @EJB
    private RoleFacade ejbFacade;
    public RoleController() {
    }

    public SelectItem[] getItemsAvailableSelectMany() {
        for (Object x : ejbFacade.findAll()) {
            items[i++] = new SelectItem(x, x.toString());
        }
        return items;
    }

    @FacesConverter(forClass = Role.class)
    public static class RoleControllerConverter implements Converter {

        @Override
        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            System.out.println("Role::Submitted: String:  "+value);
            if (value == null || value.length() == 0) {
                return null;
            }
            RoleController controller = (RoleController) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "roleController");
            System.out.println("Role::Object of Submitted: String:  "+controller.getRole(getKey(value)));
            return controller.getRole(getKey(value));
        }

        java.lang.Integer getKey(String value) {
            java.lang.Integer key;
            key = Integer.valueOf(value);
            return key;
        }

        String getStringKey(java.lang.Integer value) {
            StringBuilder sb = new StringBuilder();
            sb.append(value);
            return sb.toString();
        }

        @Override
        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof Role) {
                Role o = (Role) object;
                return getStringKey(o.getId());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + Role.class.getName());
            }
        }

    }

}

create.jsp

<f:view>
     <h:form id="groupForm">
        <h:inputText id="name" value="${groupController.selected.name}" />
        <h:inputText id="description" value="${groupController.selected.description}"  />
        <h:selectManyListbox id="roles" value="${groupController.selected.roles}"  >
             <f:selectItems value="${roleController.itemsAvailableSelectMany}" var="role" itemValue="${role}" itemLabel="${role.label}" >
        </h:selectManyListbox>

        <b:commandButton action="${groupController.createGroup}" value="SAVE"  />
     </h:form>
 </f:view>

Each time I click on the SAVE Button i get "groupForm:roles: Validation Error: Value is not valid" and "********Start Create Group***********" is never printed

Paullo
  • 2,038
  • 4
  • 25
  • 50

1 Answers1

1

Could you try it like this instead:

public List<Role> getItemsAvailableSelectMany() {
    return roleFacade.findAll();
}

<f:selectItems value="${groupController.itemsAvailableSelectMany}"
               var="role"
               itemValue="${role}"
               itemLabel="${role.xxx}"/>

(whatever property you want shown from role entity in the last line).

Plus the converter is needed, if in doubt this one seems nice.

Community
  • 1
  • 1
Jaqen H'ghar
  • 4,305
  • 2
  • 14
  • 26