how to make a <p:pickList/> return a list of objects?

UI Components for JSF
Post Reply
caioatiladf
Posts: 39
Joined: 21 Jun 2011, 18:41

22 Jun 2011, 21:56

good afternoon everyone.

I wonder about the <p:pickList/>. I did everything perfect and runs the site but does not pass the object, just a string. If I put in a ITEMVALUE a STRING, he correctly returns the values ??that were on the other side of the list, the selected values. If the ITEMVALUE I try to pass an object the following error appears: [entidades.CategoriaProduto can not be cast to java.lang.String].

Anyone know how I can pass the object and not a string? INT is not going well, just a string.

My codes...

bean:

Code: Select all


public class ControleProduto {
	Produto Produto = new Produto();
	public List<Produto> lista = new ArrayList<Produto>();
	RepositorioJPA<Produto> jpa = new RepositorioJPA<Produto>();
	private UIData objDataTableProduto;

	private ControleCategoriaProduto dao = new ControleCategoriaProduto();	
	private List<CategoriaProduto> source = new ArrayList<CategoriaProduto>();	
	private List<CategoriaProduto> target = new ArrayList<CategoriaProduto>();	
	private DualListModel<CategoriaProduto> categorias;

	
	
	public ControleProduto(){		
		source = dao.listaCategoriaProdutos();		
		categorias = new DualListModel<CategoriaProduto>(source, target);
	}
		
	
	public String teste() {		
		System.out.println("source:" + getCategorias().getSource());
		System.out.println("target:" + getCategorias().getTarget());	
		
		return "List";
	}

	
	public Produto getProduto() {
		return Produto;
	}

	public void setProduto(Produto Produto) {
		this.Produto = Produto;
	}

	public UIData getObjDataTableProduto() {
		return objDataTableProduto;
	}

	public void setObjDataTableProduto(UIData objDataTableProduto) {
		this.objDataTableProduto = objDataTableProduto;
	}
	
	public DualListModel<CategoriaProduto> getCategorias() {
		return categorias;
	}
	public void setCategorias(DualListModel<CategoriaProduto> categorias) {
		this.categorias = categorias;
	}
	public List<CategoriaProduto> getTarget() {
		return target;
	}
	public void setTarget(List<CategoriaProduto> target) {
		this.target = target;
	}
	public List<CategoriaProduto> getSource() {
		return source;
	}
	public void setSource(List<CategoriaProduto> source) {
		this.source = source;
	}

}



my xhtml:

Code: Select all



<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:p="http://primefaces.prime.com.tr/ui"
	xmlns:f="http://java.sun.com/jsf/core">

<h:form>


			<p:tabView effect="opacity" effectDuration="normal"
				style="border:0px">

				<p:tab title="Cadastrar">
				

								<p:pickList value="#{controleProduto.categorias}"
									var="cat" itemLabel="#{cat.catNome}" itemValue="#{cat.catNome}"	effect="bounce">
									 
									<f:facet name="sourceCaption">Categorias Disponíveis</f:facet>
									<f:facet name="targetCaption">Categorias Pertencentes</f:facet>

								</p:pickList>



                                                              <p:commandButton value="Testes" onclick="teste.show()"
								type="button" image="ui-icon ui-icon-trash" />

							<p:confirmDialog widgetVar="teste" header="Confirmação"
								message="teste?" showEffect="explode"
								hideEffect="explode" severity="alert" modal="false">

								<p:commandButton value="Sim" update="mensagens"
									onclick="teste.hide()"
									actionListener="#{controleProduto.teste}" />

								<p:commandButton value="Não" onclick="teste.hide()"
									type="button" />

							</p:confirmDialog>


				</p:tab>
			</p:tabView>
		</h:form>
</html>


the way up is working, but returns a list with the names of the categories, I would return a list of category objects.

If I change the following line:

Code: Select all

<p:pickList value="#{controleProduto.categorias}"
									var="cat" itemLabel="#{cat.catNome}" itemValue="#{cat.catNome}"	effect="bounce">
to:

<p:pickList value="#{controleProduto.categorias}"
									var="cat" itemLabel="#{cat.catNome}" itemValue="#{cat}"	effect="bounce">
appears the error mentioned above.

I appreciate the help.

MilenaMMay
Posts: 13
Joined: 31 May 2011, 17:15

23 Jun 2011, 08:41

I think it's the same as with every JSF component that should return objects (select menus etc.): You have to use a converter.

PIcklist

<p:pickList value="#{controller.myPickList}"
var="myItem"
itemLabel="#{myItem.description}" itemValue="#{myItem}"
converter="#{myConverter}">

faces-config

<converter>
<converter-id>myConverter</converter-id>
<converter-class>com.mypackage.MyConverter</converter-class>
</converter>

Or like this (more convenient if you just convert your objects in one manner):


PIcklist

<p:pickList value="#{controller.myPickList}"
var="myItem"
itemLabel="#{myItem.description}" itemValue="#{myItem}">

faces-config

<converter>
<converter-for-class>com.mypackage.MyItem</converter-for-class>
<converter-class>com.mypackage.MyConverter</converter-class>
</converter>

if you need to use a managedBean e.g. a service in yur converter, try this: http://stackoverflow.com/questions/6135 ... sconverter

caioatiladf
Posts: 39
Joined: 21 Jun 2011, 18:41

28 Jun 2011, 16:56

Hello, thanks for the help.

I've been looking at other sites also showed me and all use the convert.

Entertaining and tried to use a new error appeared.
Error: "Expression error: Object named: categoriaProdutoConverter not found."

It is as if the system had not found the convert. I'll tell you what I changed in the code mentioned in my first question for the use of the convert.



1 - I created a class called CategoriaProdutoConverter:

Code: Select all


primepad.converters package;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

import primepad.entidades.CategoriaProduto;
import primepad.repositorio.RepositorioJPA;

public class CategoriaProdutoConverter implements Converter {

CategoriaProduto CategoriaProduto category = new ();
RepositorioJPA <CategoriaProduto> RepositorioJPA <CategoriaProduto> jpa = new ();

@ Override
    public Object getAsObject (FacesContext context, UIComponent component, String value) throws ConverterException {
        try {
        
        int id = Integer.parseInt (value);
            category = jpa.selecionarEntidadePorCodigo ("CategoriaProduto", "cat_codigo", id);
            return category;
            
        } Catch (Exception e) {
            return null;
        }
    }
    
@ Override
    public String getAsString (FacesContext context, UIComponent component, Object value) throws ConverterException {
        if (value! = null) {
            return Value.ToString ();
        Else {}
            return null;
        }
    }
}
[/ Code]

This class return "theoretically" my entity.

2 - I changed the xhtml
[Code]
<P: picklist value = "# { controleProduto.categorias}"
var = "cat" itemLabel = "# { cat.catNome}" itemValue = "# {cat}"
convert = "categoriaProdutoConverter">

Available Categories <f:facet name="sourceCaption"> </ f: facet>
Categories <f:facet name="targetCaption"> Owned </ f: facet>
</ P: picklist>
[/ Code]


3 - put the following code in the web config:

[Code]
<converter>
<converter-id> categoriaProdutoConverter </ converter-id>
<converter-class> primepad.converters.CategoriaProdutoConverter </ converter-class>
</ Convert>
[/ Code]

This insertion of code in web-config generated an error. I do not understand why, the exact path of the class is the same: "primepad.converters.CategoriaProdutoConverter."

the error is this: "An internal error occurred during:" Update web xml ".
org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature 'convert' not found. "


Anyone know why? I'm doing right? I noticed that several people are experiencing the same problem.
Thank you all.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 33 guests