首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Primefaces PickList中的转换器

Primefaces PickList中的转换器
EN

Stack Overflow用户
提问于 2011-08-15 02:04:14
回答 5查看 22.2K关注 0票数 2

我使用的是Primefaces的PickList,但我不能让它工作。我的问题是Converter。我遵循了另一个帖子的指示,但徒劳无功。

这是我的小脸蛋

代码语言:javascript
复制
<p:pickList value="#{customerBean.preferredCategories}" var="category"
   itemLabel="#{category.description}" itemValue="#{category}" converter="#{categoryConverter}">
</p:pickList>

这里是我的自定义转换器

代码语言:javascript
复制
@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)和一个描述( String )组成,我希望源列表和目标列表都显示描述字符串,并将选定的类别设置为我的bean中的Category列表。两个列表都被正确加载到bean中,并且preferredCategories中填充了DualListModel。问题是PickList甚至没有被渲染。什么都没有发生,没有显示错误,页面只是在转到PickList时停止渲染,我认为这是因为错误地使用了转换器。哪种方法是实现我的这个案例的正确方法?

谢谢。

EN

回答 5

Stack Overflow用户

发布于 2011-08-15 02:24:16

我认为

代码语言:javascript
复制
@FacesConverter(forClass=CategoryLevelView.class,value="categoryConverter")
public class CategoryConverter implements Converter {

应该是

代码语言:javascript
复制
@FacesConverter(forClass=Category.class,value="categoryConverter")
public class CategoryConverter implements Converter {

forClass的值更改为Category.class

而且,您不需要在<p:picklist中提及converter属性值。

票数 2
EN

Stack Overflow用户

发布于 2014-03-11 22:45:31

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

代码语言:javascript
复制
@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();
}
}
票数 2
EN

Stack Overflow用户

发布于 2011-08-15 02:15:55

在这一行中:

代码语言:javascript
复制
@FacesConverter(forClass=CategoryLevelView.class,value="categoryLevelConverter")

看起来您正在尝试将转换器id设置为categoryLevelConverter

在Facelet的这一行中:

代码语言:javascript
复制
converter="#{categoryConverter}"

转换器id不匹配。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7058667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档