Hebert_Coelho 22 de dez. de 2011
Tem como você postar a classe customer e o método #{installedProductsOverviewBean.completeCustomer}?
MaYaRa_SaN 22 de dez. de 2011
voce add seu converter no faces-config.xml?
fernandosc 22 de dez. de 2011
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?
Hebert_Coelho 22 de dez. de 2011
a classe Customer tem get/set cnpj?
MaYaRa_SaN 22 de dez. de 2011
Desculpa, nao reparei na anotation, com ela na classe do converter nao precisa colocar no faces-config não.
fernandosc 22 de dez. de 2011
Hebert_Coelho 22 de dez. de 2011
jakefrog:
a classe Customer tem get/set cnpj?
Sim. Tem como colocar o get/set? soh para eliminar essa dúvida e ir para outras verificações. [=
fernandosc 22 de dez. de 2011
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 ;
}
}
MaYaRa_SaN 22 de dez. de 2011
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 ++ );
}
}
Hebert_Coelho 22 de dez. de 2011
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
fernandosc 22 de dez. de 2011
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.
fernandosc 23 de dez. de 2011
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>