【问题标题】:Converter in Primefaces PickListPrimefaces PickList 中的转换器
【发布时间】:2011-10-26 21:13:30
【问题描述】:

我正在使用 Primefaces 的 PickList,但无法正常工作。我的问题是转换器。我按照另一个帖子的指示,但徒劳无功。

这是我的小脸

<p:pickList value="#{customerBean.preferredCategories}" var="category"
   itemLabel="#{category.description}" itemValue="#{category}" converter="#{categoryConverter}">
</p:pickList>

这里是我的自定义转换器

@FacesConverter(forClass=CategoryLevelView.class,value="categoryLevelConverter")
public class CategoryConverter implements Converter {

    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return String.valueOf(((Category) value).getId());
    }

    public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
        Category category = new Category();
        category.setId(Integer.parseInt(value));   
        return category;
    }
}

Category 由一个 id (int) 和一个 description (String) 组成 我希望源列表和目标列表都显示描述字符串,并将所选类别设置为我的 bean 中的类别列表。两个列表都正确加载到 bean 中,并且 DualListModel 填充到了preferredCategories 中。问题是 PickList 甚至没有被渲染。没有任何反应,没有显示错误,页面在轮流到达 PickList 时停止渲染,我认为这是因为转换器使用错误。哪个是实施我的案例的正确方法?

谢谢。

【问题讨论】:

  • 您可以在任何答案下添加评论。只需单击显示add comment 的链接。

标签: jsf-2 primefaces converter


【解决方案1】:

我认为

@FacesConverter(forClass=CategoryLevelView.class,value="categoryConverter")
public class CategoryConverter implements Converter {

应该是

@FacesConverter(forClass=Category.class,value="categoryConverter")
public class CategoryConverter implements Converter {

forClass 的值更改为Category.class

而且,您不需要在&lt;p:picklist 中提及converter 属性的值。

【讨论】:

    【解决方案2】:

    这在没有 ArrayIndexOutOfBounds 异常的情况下工作。

    @FacesConverter("PickListConverter")
    
    public class PickListConverter implements Converter {
    
    public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {
        PickList p = (PickList) component;
        DualListModel dl = (DualListModel) p.getValue();
        for (int i = 0; i < dl.getSource().size(); i++) {
            if (dl.getSource().get(i).toString().contentEquals(submittedValue)) {
                return dl.getSource().get(i);
            }
        }
        for (int i = 0; i < dl.getTarget().size(); i++) {
            if (dl.getTarget().get(i).toString().contentEquals(submittedValue)) {
                return dl.getTarget().get(i);
            }
        }
        return null;
    }
    
    public String getAsString(FacesContext facesContext, UIComponent component, Object value) {
        PickList p = (PickList) component;
        DualListModel dl = (DualListModel) p.getValue();
        // return String.valueOf(dl.getSource().indexOf(value));
        return value.toString();
    }
    }
    

    【讨论】:

      【解决方案3】:

      在这一行:

      @FacesConverter(forClass=CategoryLevelView.class,value="categoryLevelConverter")
      

      您似乎正在尝试将转换器 ID 设置为 categoryLevelConverter

      在 Facelet 的这一行中:

      converter="#{categoryConverter}"
      

      转换器 id 不匹配。

      【讨论】:

        【解决方案4】:

        我做了一个简单的转换器,它适用于 Primefaces PickList 中的所有值:

        @FacesConverter("PickListConverter")
        public class PickListConverter implements Converter{
        
            public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {
                PickList  p = (PickList) component;
                DualListModel dl = (DualListModel) p.getValue();
                return dl.getSource().get(Integer.valueOf(submittedValue));
            }
        
            public String getAsString(FacesContext facesContext, UIComponent component, Object value) {
                PickList  p = (PickList) component;
                DualListModel dl = (DualListModel) p.getValue();
                return  String.valueOf(dl.getSource().indexOf(value));
            }
        }
        

        【讨论】:

        • 此代码使用选项列表的索引(0、1、2 等)来识别哪些选项列表的条目已从源移动到目标。虽然这可能是处理选项列表和转换器的最简单和优雅的方式,但 getSource.indexOf(value) 有时会返回 -1(当 value 已经在 Target 中时),因此您最终会在 getAsObject 中遇到 ArrayIndexOutOfBounds 异常.我想知道作者是如何使这个功能起作用的。
        【解决方案5】:

        我不知道你是否解决了你的问题,如果没有,你可以试试这个。 在 getAsObject 方法中,您所做的是创建一个新的类别对象并设置其 id 并返回它。我认为您应该在这里做的是从具有该 id 的数据库中获取类别,然后将其返回。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-07-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多