Converter in Primefaces PickList
Converter in Primefaces 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
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.
Answer by Jim Tough for Converter in Primefaces PickList
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.
Answer by Bhesh Gurung for Converter in Primefaces PickList
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
Answer by khizAz for Converter in Primefaces PickList
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.
Answer by MariemG for Converter in Primefaces PickList
try this converter , primefacesconverterexample I think that it should work.
Answer by voodoo for Converter in Primefaces PickList
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)); } }
Answer by user3406691 for Converter in Primefaces PickList
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(); } }
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment