Primefaces PickList 中的转换器

发布于 2024-11-29 15:55:49 字数 1114 浏览 0 评论 0 原文

我正在使用 Primefaces 的 PickList,但无法使其工作。我的问题是转换器。我按照另一篇文章的指示进行操作,但没有成功。

这是我的 Facelet

<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;
    }
}

类别由 id(int)和描述(String)组成 我希望源列表和目标列表都显示描述字符串,并将选定的类别设置为我的 bean 中的类别列表。两个列表都已正确加载到 bean 中,并且 DualListModel 已填充到 PreferredCategories 中。问题是 PickList 甚至没有呈现。什么也没有发生,没有显示错误,当轮到 PickList 时页面只是停止渲染,我认为这是因为转换器的错误使用。哪种方法是实施我的这个案例的正确方法?

谢谢。

I'm using Primefaces' PickList and I can't make it work. My problem is Converter. I followed the directions of another post, but in vain.

Here is my facelet

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

and here my custom converter

@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 is composed by an id (int) and a description (String)
I want both source and target Lists to display the description String, and the selected categories set as a List of Category in my bean. Both lists are correctly loaded in the bean and the DualListModel is populated in preferredCategories. The problem is the PickList is not even rendered. Nothing happens, no error displayed, the page just stops rendering when the turns arrives to PickList, and I think it's because a wrong usage of converter. Which would be a correct way to implement my this case?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北笙凉宸 2024-12-06 15:55:49

我认为

@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

而且,您不需要在 中提及 converter 属性的值。

I think

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

should be

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

Change value of forClass to Category.class.

And, you should not need to mention the value of converter attribute in <p:picklist.

情泪▽动烟 2024-12-06 15:55:49

这可以在没有 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();
}
}

This working without an ArrayIndexOutOfBounds exception.

@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();
}
}
浪漫人生路 2024-12-06 15:55:49

在这一行中:

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

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

在 Facelet 的这一行中:

converter="#{categoryConverter}"

转换器 id 不匹配。

In this line:

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

It looks like you're trying to set the converter id to categoryLevelConverter.

In this line of your Facelet:

converter="#{categoryConverter}"

The converter id does not match.

薄荷→糖丶微凉 2024-12-06 15:55:49

我制作了一个简单的转换器,它适用于 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));
    }
}

i made one simple converter and it works well with all values in 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));
    }
}
£烟消云散 2024-12-06 15:55:49

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

I do not know if you have solved your problem but if not, you can try this.
In the getAsObject method , what you are doing is creating a new category object and setting its id and returning it. I think what you should do here is get the category from the database with that id and then return that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文