[RESOLVIDO]Componente p:autoComplete não encontra Converter - JSF 2.0

12 respostas
F
Estou tentando usar o componente p:autoComplete do Primefaces 3.0RC2, porém ele da o seguinte erro:
itemLabel="#{customer.cnpj}": Property 'cnpj' not found on type java.lang.String
Mesmo o cnpj sendo um String, dai estou tentando usar um converter, mas da um erro acusando que não esta encontrando.
<p:autoComplete id="customerAutoComplete" value="#{installedProductsOverviewBean.selectedCustomerCnpj}" converter="customerConverter"
        completeMethod="#{installedProductsOverviewBean.completeCustomer}" var="customer" itemValue="#{customer.cnpj}" itemLabel="#{customer.cnpj}" >        
        
        <p:ajax event="itemSelect" listener="#{installedProductsOverviewBean.handleSelect}" update="installedProductsPanel"/>
        
      </p:autoComplete>
Converter:
import javax.faces.component.UIComponent;  
import javax.faces.context.FacesContext;  

import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@FacesConverter(value="customerConverter")
public class CustomerConverter implements Converter{

	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
		return value;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) {
		if (value == null || value.equals("")) {  
            return "";  
        } else {  
            return String.valueOf(((Customer) value).getCnpj());  
        }  
	} 
	   
       
}

12 Respostas

H

Tem como você postar a classe customer e o método #{installedProductsOverviewBean.completeCustomer}?

M

voce add seu converter no faces-config.xml?

F
Classe Customer:
@Entity
public class Customer implements Serializable{
  
  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

@Id
  @Column(name="cnpj", length=20)
  private String cnpj;
  
  @Column(name="blocked")
  private boolean isBlocked;
  
  @OneToMany
  @JoinColumn(name="customer_cnpj")
  private Set<CustomerProduct> customerProducts;
  
  @ManyToMany
  @JoinTable(name="installed_product_customer", 
      joinColumns={@JoinColumn(name="customer_cnpj")},
      inverseJoinColumns={@JoinColumn(name = "installed_product_id")})
  private Set<InstalledProduct> installedProducts;
  
  public Customer() {
  }
  
  public Customer(String cnpj) {
    this.cnpj = cnpj;
  }

  @Override
  public String toString() {
    return cnpj;
  }...
Complete Customer:
public List<Customer> completeCustomer(String cnpjQuery) {
		log.debug("Making a customer autocomplete query " + cnpjQuery);
		return customerService.findCustomerBySuggestions(cnpjQuery);
	}

Mesmo usando JSF 2.0 é necessário configurar Converter no faces-config.xml?

H

a classe Customer tem get/set cnpj?

M

Desculpa, nao reparei na anotation, com ela na classe do converter nao precisa colocar no faces-config não.

F

Sim.

H

Sim. Tem como colocar o get/set? soh para eliminar essa dúvida e ir para outras verificações. [=

F
Segue:
@Entity
public class Customer implements Serializable{
  
  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

@Id
  @Column(name="cnpj", length=20)
  private String cnpj;
  
  @Column(name="blocked")
  private boolean isBlocked;
  
  @OneToMany
  @JoinColumn(name="customer_cnpj")
  private Set<CustomerProduct> customerProducts;
  
  @ManyToMany
  @JoinTable(name="installed_product_customer", 
      joinColumns={@JoinColumn(name="customer_cnpj")},
      inverseJoinColumns={@JoinColumn(name = "installed_product_id")})
  private Set<InstalledProduct> installedProducts;
  
  public Customer() {
  }
  
  public Customer(String cnpj) {
    this.cnpj = cnpj;
  }

  @Override
  public String toString() {
    return cnpj;
  }

  public Set<CustomerProduct> getCustomerProducts() {
    return customerProducts;
  }

  public void setCustomerProducts(Set<CustomerProduct> customerProducts) {
    this.customerProducts = customerProducts;
  }

  public String getCnpj() {
    return cnpj;
  }

  public void setCnpj(String cnpj) {
    this.cnpj = cnpj;
  }

  public boolean isBlocked() {
    return isBlocked;
  }

  public void setBlocked(boolean isBlocked) {
    this.isBlocked = isBlocked;
  }

  public Set<InstalledProduct> getInstalledProducts() {
    return installedProducts;
  }

  public void setInstalledProducts(Set<InstalledProduct> installedProducts) {
    this.installedProducts = installedProducts;
  }
}
M

isso está certo???

@Override  
    public Object getAsObject(FacesContext context, UIComponent component, String value) {  
        return value;  
    }
voce está devolvendo o proprio string como obj...

segue um converter meu:

@FacesConverter(value = "IndexedConverter")
public class IndexedConverter implements Converter {

  private int index;

   public Object getAsObject(FacesContext facesContext, UIComponent
uicomp, String value) {
       List<SelectItem> items = new ArrayList<SelectItem>();
       List<UIComponent> uicompList = uicomp.getChildren();
       for(UIComponent comp: uicompList){
           if(comp instanceof UISelectItems){
               items.addAll((List<SelectItem>)
((UISelectItems)comp).getValue());
           }
       }
       return "-1".equals(value) ? null :
items.get(Integer.valueOf(value)).getValue();
   }

   public String getAsString(FacesContext facesContext, UIComponent
uicomp, Object entity) {
       return entity == null ? "-1" : String.valueOf(index++);
   }
}
H

MaYaRa_SaN:
isso está certo???

@Override public Object getAsObject(FacesContext context, UIComponent component, String value) { return value; }
voce está devolvendo o proprio string como obj…


Boa. Pode ser isso mesmo.

Mano, um exemplo de passo a passo para converter se acha aqui: JSF: Converter e Bean Auto Complete

F
Alterei o meu converter e ficou assim:
@FacesConverter(value="customerConverter")
public class CustomerConverter implements Converter{

	@Autowired
	CustomerService customerService;
	
	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
		if(value == null || value.equals("")){			
			return null;
		}else{
			return customerService.findById(value);			
		}
		
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) {
		if (value == null || value.equals("")) {  
            return "";  
        } else {  
            return String.valueOf(((Customer) value).getCnpj());  
        }  
	} 
	   
       
}
Mas o problema é que ele nem encontra, da o seguinte erro:
Erro de expressão: Objeto denominado: customerConverter não encontrado.
F

Encontrei o meu poblema, estou usando Spring 3.0 na minha aplicação, basta fazer o seguinte:

Converter:
@Component(value="customerConverter")
public class CustomerConverter implements Converter{  
  
    @Override  
    public Object getAsObject(FacesContext context, UIComponent component, String value) {  
        return value;  
    }  
  
    @Override  
    public String getAsString(FacesContext context, UIComponent component, Object value) {  
        if (value == null || value.equals("")) {    
            return "";    
        } else {    
            return String.valueOf(((Customer) value).getCnpj());    
        }    
    }   
         
         
}
E para chamar:
<p:autoComplete id="customerAutoComplete" value="#{installedProductsOverviewBean.selectedCustomerCnpj}" converter="#{customerConverter}"  
        completeMethod="#{installedProductsOverviewBean.completeCustomer}" var="customer" itemValue="#{customer.cnpj}" itemLabel="#{customer.cnpj}" >          
          
        <p:ajax event="itemSelect" listener="#{installedProductsOverviewBean.handleSelect}" update="installedProductsPanel"/>  
          
      </p:autoComplete>
Criado 22 de dezembro de 2011
Ultima resposta 23 de dez. de 2011
Respostas 12
Participantes 3