Forum Stats

  • 3,782,437 Users
  • 2,254,645 Discussions
  • 7,880,078 Comments

Discussions

JSF Conversion error for h:selectManyListbox

843844
843844 Member Posts: 40,416
edited Aug 14, 2008 4:59PM in JavaServer Faces

There's an exception (*Conversion error occurred*) from JSF on <h:selectManyListbox> component for id availableUsers which is bound to myBean*.*roleId


Now myBean*.*roleId is of Long datatype and so is the usersList*.*SelectItem*.*value. So, would not be any datatype mismatch. Tried using a converter too with no luck.


If you have come across any such issue, please do let me know, thanks!



-----------------------------------------


Exception


-----------------------------------------



[email protected]]


[8/14/08 1:59:44:645 IST] 00000061 jsf E com.sun.faces.context.FacesContextImpl addMessage Adding Message[sourceId=emailmessage:*availableUsers* ,summary=*Conversion error occurred*.)


[8/14/08 1:59:44:661 IST] 00000061 jsf E com.sun.faces.context.FacesContextImpl addMessage Adding Message[sourceId=emailmessage:selectedUsers,summary=Conversion error occurred.)



-----------------------------------------

*
JSF

*

-----------------------------------------


<h:selectManyListbox id="*availableUsers* " value="#{myBean.roleId}" converter="javax.faces.Long"


binding="#{myBean.usersListBox}" size="5" style="width: 138px; height: 102px">


<f:selectItems id="usersItems" value="#{myBean.usersList}"


binding="#{myBean.usersListBoxSelectItems}" />


</h:selectManyListbox></td>



-----------------------------------------

*
CLASS

*

-----------------------------------------

*{code}*
*class MyBean*

*private Long roleId;*

*private List usersList;*
*{code}*

usersList is an arraylist of SelectItem where each SelectItem is composed of a Long value and a String label as per the convention.

Comments

  • 843844
    843844 Member Posts: 40,416
    The value of the h:selectMany* components must be bound with an array or a collection. Simply because you can select more than one value. If you want to select only one value, then use h:selectOne* component instead and keep the properties as is.
  • 843844
    843844 Member Posts: 40,416
    Thanks BaluC for responding.

    Did you mean that in the following code, myBean.roleId has to be necessarily an arraylist too apart from the fact that myBean.availableUsersList is infact an ArrayList from where the selectitems would be populated?

    <h:selectManyListbox id="availableUsers" value="#{myBean.roleId}" converter="javax.faces.Long"
  • 843844
    843844 Member Posts: 40,416
    edited Aug 14, 2008 7:25AM
    How would you collect multiple values otherwise? Make it a Long[] or List<Long> instead of Long.
  • 843844
    843844 Member Posts: 40,416
    I tried using a List<Long>, but getting another error now, any help would be greatly appreciated, thanks!

    Error:
    [8/15/08 0:11:32:692 IST] 0000007e jsf E com.sun.faces.context.FacesContextImpl addMessage Adding Message[sourceId=emailmessage:selectedUsers,summary=Validation Error: Value is not valid)


    JSF Code:
    h:selectManyListbox id="selectedUsers" binding="#{myBean.selectedUsersListBox}" value="#{myBean.recipientIdList}" size="5"
    converter="#{myBean.recipientIdListConverter}" style="width: 138px; height: 102px">
    <f:selectItems id="selectedUsersItem" value="#{myBean.recipientList}" binding="#{myBean.selectedUsersListBoxSelectItems}" />
    </h:selectManyListbox>


    backing Bean Code for class myBean:

    {code}public List recipientList;
    private ArrayList recipientIdList;
    private HtmlSelectManyListbox selectedUsersListBox;
    private UISelectItems selectedUsersListBoxSelectItems;


    public Converter getRecipientIdListConverter() {
    return new Converter() {
    public Object getAsObject(FacesContext facesContext,
    UIComponent component, String value)
    throws ConverterException {
    if (value == null)
    return null;

    for (Iterator i = recipientIdList.iterator(); i.hasNext();) {
    Long item = (Long) i.next();
    String val = String.valueOf(item);
    if (val.equals(value)) {
    return item;
    }
    }
    return null;
    }

    public String getAsString(FacesContext facesContext,
    UIComponent component, Object object)
    throws ConverterException {
    if (object instanceof SelectItem) {
    return ((SelectItem) object).getValue().toString();
    } else if (object instanceof Long) {
    return String.valueOf(object);
    } else {
    return object.toString();
    }
    }

    };
    }{code}
  • 843844
    843844 Member Posts: 40,416
    IceLander wrote:
    Error:
    [8/15/08 0:11:32:692 IST] 0000007e jsf E com.sun.faces.context.FacesContextImpl addMessage Adding Message[sourceId=emailmessage:selectedUsers,summary=Validation Error: Value is not valid)


    JSF Code:
    h:selectManyListbox id="selectedUsers" binding="#{myBean.selectedUsersListBox}" value="#{myBean.recipientIdList}" size="5"
    converter="#{myBean.recipientIdListConverter}" style="width: 138px; height: 102px">
    <f:selectItems id="selectedUsersItem" value="#{myBean.recipientList}" binding="#{myBean.selectedUsersListBoxSelectItems}" />
    </h:selectManyListbox>
    You need to make sure that the values of the selected items (the recipientIdList) are part of the values of the provided selectItems list (the recipientList).
  • 843844
    843844 Member Posts: 40,416
    So have another POJO composed of recipientIdList and recipientList?
  • 843844
    843844 Member Posts: 40,416
    edited Aug 14, 2008 4:59PM
    No, I don't mean that. If you have a List<SelectItem> for the dropdown with the values Long 1, Long 2 and Long 3, then you should make sure that the List<Long> for the selected items is null or contains nothing else than Long 1, Long 2 and/or Long 3. If you have for example a Long 4 in it, which isn't available in the dropdown values, then you will get this validation error.

    By the way, I did a closer look for the converter. And that logic really doesn't make any sense. You don't need to worry about SelectItem in the converter, but about its values. Leave it away. JSF has just built in converter for Long <--> String which will be invoked 'automagically'.
This discussion has been closed.