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.
[code]<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>[/code]
Converter:
[code]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());
}
}
}[/code]
Tem como você postar a classe customer e o método #{installedProductsOverviewBean.completeCustomer}?
voce add seu converter no faces-config.xml?
Classe Customer:
[code]@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 customerProducts;
@ManyToMany
@JoinTable(name=“installed_product_customer”,
joinColumns={@JoinColumn(name=“customer_cnpj”)},
inverseJoinColumns={@JoinColumn(name = “installed_product_id”)})
private Set installedProducts;
public Customer() {
}
public Customer(String cnpj) {
this.cnpj = cnpj;
}
@Override
public String toString() {
return cnpj;
}…[/code]
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?
a classe Customer tem get/set cnpj?
Desculpa, nao reparei na anotation, com ela na classe do converter nao precisa colocar no faces-config não.
Sim.[/quote] Tem como colocar o get/set? soh para eliminar essa dúvida e ir para outras verificações. [=
Segue:
[code]
@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 customerProducts;
@ManyToMany
@JoinTable(name=“installed_product_customer”,
joinColumns={@JoinColumn(name=“customer_cnpj”)},
inverseJoinColumns={@JoinColumn(name = “installed_product_id”)})
private Set installedProducts;
public Customer() {
}
public Customer(String cnpj) {
this.cnpj = cnpj;
}
@Override
public String toString() {
return cnpj;
}
public Set getCustomerProducts() {
return customerProducts;
}
public void setCustomerProducts(Set 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 getInstalledProducts() {
return installedProducts;
}
public void setInstalledProducts(Set installedProducts) {
this.installedProducts = installedProducts;
}
}[/code]
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:
[code]@FacesConverter(value = “IndexedConverter”)
public class IndexedConverter implements Converter {
private int index;
public Object getAsObject(FacesContext facesContext, UIComponent
uicomp, String value) {
List items = new ArrayList();
List uicompList = uicomp.getChildren();
for(UIComponent comp: uicompList){
if(comp instanceof UISelectItems){
items.addAll((List)
((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++);
}
}
[/code]
[quote=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…[/quote]
Boa. Pode ser isso mesmo.
Mano, um exemplo de passo a passo para converter se acha aqui: JSF: Converter e Bean Auto Complete
Alterei o meu converter e ficou assim:
[code]@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());
}
}
}[/code]
Mas o problema é que ele nem encontra, da o seguinte erro:
Erro de expressão: Objeto denominado: customerConverter não encontrado.
Encontrei o meu poblema, estou usando Spring 3.0 na minha aplicação, basta fazer o seguinte:
Converter:
[code]@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());
}
}
} [/code]
E para chamar:
[code]<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> [/code]