我正在使用 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.
发布评论
评论(5)
我认为
应该将
forClass
的值更改为Category.class
。而且,您不需要在 中提及
converter
属性的值。I think
should be
Change value of
forClass
toCategory.class
.And, you should not need to mention the value of
converter
attribute in<p:picklist
.这可以在没有 ArrayIndexOutOfBounds 异常的情况下工作。
This working without an ArrayIndexOutOfBounds exception.
在这一行中:
您似乎正在尝试将转换器 ID 设置为
categoryLevelConverter
。在 Facelet 的这一行中:
转换器 id 不匹配。
In this line:
It looks like you're trying to set the converter id to
categoryLevelConverter
.In this line of your Facelet:
The converter id does not match.
我制作了一个简单的转换器,它适用于 Primefaces PickList 中的所有值:
i made one simple converter and it works well with all values in Primefaces PickList:
不知道你的问题有没有解决,如果没有的话,你可以试试这个。
在
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.