Miloš Rašić
Miloš Rašić

Reputation: 2279

Problems with p:orderlist and session scoped backing bean

I have a session scoped managed bean called MyController. It has a reference to a POJO called MyModel. MyModel contains an ArrayList of some other POJOs which I bind to a p:orderlist like this:

                <p:orderList value="#{myController.myModel.list}" var="item" itemValue="#{item}" converter="#{itemConverter}"">
                    <f:facet name="caption">some title</f:facet>

                    <p:column>
                        #{item.text}
                    </p:column>
                </p:orderList>

My converter looks like this:

@ManagedBean
@RequestScoped
public class ItemConverter implements Converter {

    @ManagedProperty(value="#{myController.myModel.list}")
    private List<Item> list;


    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        Iterator i = list.iterator();
        int id = Integer.parseInt(value);

        while (i.hasNext()) {
            Object currentObject = i.next();
            if (((Item) currentObject).getId() == id) {
                return currentObject;
            }
        }

        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return Integer.toString(((Item) value).getId());
    }

}

I have built an interface and methods in myController to dynamically add items to the orderlist. When I first add an item, it seems to work fine, the list is updated and shows the new item. However, when I add the second item, the dialog that contains the list disappears and cannot be shown even by calling its show() method from JavaScript console. By debugging, I have discovered that when adding the second item, my list has become ArrayList of String instead of ArrayList of Item as it was declared and defined. It contains the String that would be returned by getAsString() method of my converter for the first item I have added to the list. Naturally, adding the second item fails since I'm trying to add an Item to a List of Strings.

How is this possible? I thought Java would never allow a List of Item to be replaced by List of String. Is it even possible to use orderlist without having it overwrite the source list with it's own list of strings? Isn't the job of converter to convert those strings back to objects in the first place? Would it be possible to construct orderlist on server side from List of Items and then use the binding attribute and value attribute to map to another List of String? If it is possible, how would I construct the orderlist on the server side?

EDIT: Fixed a problem with double call of i.next() in getAsObject(), but still getting a list of strings. Also, trying to manually instantiate the converter with context.getApplication().evaluateExpressionGet(context, "#{itemConverter}", itemConverter.class); returns null.

Upvotes: 0

Views: 921

Answers (1)

Miloš Rašić
Miloš Rašić

Reputation: 2279

The cause of my problem was a wrong import. I imported javax.annotations.ManagedBean instead of import javax.faces.bean.ManagedBean. So, if someone ends up on this page by googling for a solution to their problem, check your imports first.

Upvotes: 2

Related Questions