简体   繁体   English

p:orderlist和会话范围的支持bean的问题

[英]Problems with p:orderlist and session scoped backing bean

I have a session scoped managed bean called MyController. 我有一个名为MyController的会话作用域托管bean。 It has a reference to a POJO called MyModel. 它引用了一个名为MyModel的POJO。 MyModel contains an ArrayList of some other POJOs which I bind to ap:orderlist like this: MyModel包含一些其他POJO的ArrayList,我将它绑定到ap:orderlist,如下所示:

                <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. 我在myController中构建了一个接口和方法,以动态地将项目添加到订单列表中。 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. 但是,当我添加第二个项目时,包含该列表的对话框将消失,即使从JavaScript控制台调用其show()方法也无法显示。 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. 通过调试,我发现在添加第二个项目时,我的列表已成为String的ArrayList,而不是声明和定义的Item的ArrayList。 It contains the String that would be returned by getAsString() method of my converter for the first item I have added to the list. 它包含我的转换器的getAsString()方法返回的String,用于我添加到列表中的第一个项目。 Naturally, adding the second item fails since I'm trying to add an Item to a List of Strings. 当然,添加第二个项失败,因为我正在尝试将一个Item添加到字符串列表中。

How is this possible? 这怎么可能? I thought Java would never allow a List of Item to be replaced by List of String. 我以为Java永远不会允许List of Item替换为String of List。 Is it even possible to use orderlist without having it overwrite the source list with it's own list of strings? 甚至可以使用orderlist而不用它自己的字符串列表覆盖源列表? 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. 编辑:修复了在getAsObject()中双重调用i.next()的问题,但仍然得到一个字符串列表。 Also, trying to manually instantiate the converter with context.getApplication().evaluateExpressionGet(context, "#{itemConverter}", itemConverter.class); 此外,尝试使用context.getApplication().evaluateExpressionGet(context, "#{itemConverter}", itemConverter.class);手动实例化转换器context.getApplication().evaluateExpressionGet(context, "#{itemConverter}", itemConverter.class); returns null. 返回null。

The cause of my problem was a wrong import. 我的问题的原因是错误的导入。 I imported javax.annotations.ManagedBean instead of import javax.faces.bean.ManagedBean. 我导入了javax.annotations.ManagedBean,而不是导入javax.faces.bean.ManagedBean。 So, if someone ends up on this page by googling for a solution to their problem, check your imports first. 因此,如果有人通过Google搜索解决问题的方式结束此页面,请先检查您的导入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM