[quote=tiago__][quote=eduardimaa]Amigo faz assim:
cria essa classe aki o :
EntityConverter.java[code]
import java.lang.reflect.Field;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.persistence.Id;
@FacesConverter(value=“entityConverter”)
public class EntityConverter implements Converter {
@Override
public Object getAsObject(FacesContext ctx, UIComponent component,
String value) {
if (value != null) {
return component.getAttributes().get(value);
}
return null;
}
@Override
public String getAsString(FacesContext ctx, UIComponent component,
Object obj) {
if (obj != null && !"".equals(obj)) {
String id;
try {
id = this.getId(getClazz(ctx, component), obj);
if (id == null) {
id = "";
}
id = id.trim();
component.getAttributes().put(id,
getClazz(ctx, component).cast(obj));
return id;
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassCastException e) {
e.printStackTrace();
}
}
return null;
}
private Class<?> getClazz(FacesContext facesContext, UIComponent component) {
return component.getValueExpression("value").getType(
facesContext.getELContext());
}
public String getId(Class<?> clazz, Object obj) throws SecurityException,
NoSuchFieldException, IllegalArgumentException,
IllegalAccessException {
for (Field field : clazz.getDeclaredFields()) {
if ((field.getAnnotation(Id.class)) != null) {
Field privateField = clazz.getDeclaredField(field.getName());
privateField.setAccessible(true);
if (privateField.get(clazz.cast(obj)) != null) {
return (String) field.getType()
.cast(privateField.get(clazz.cast(obj))).toString();
} else {
return null;
}
}
}
return null;
}
}
[/code]
e um converter generico, serve para varios objetos.
depois la no selectOneMenu você chama ele atraves do converter=“entityConverter”
[code]<p:selectOneMenu id=“empresaOneMenu” value="#{omissaoBean.empresa}“
required=“true” converter=“entityConverter"
requiredMessage=“Selecione uma Empresa.”>
<f:selectItem itemLabel=“Selecione” noSelectionOption=“false” />
<f:selectItems value=”#{omissaoBean.listaEmpresa}” var=“empresa"
itemLabel=”#{empresa.nome_empresa}" itemValue="#{empresa}" />
<p:ajax event=“change” listener="#{omissaoBean.preencheOneMenu}“
update=”:cadastroOmissao:linhaOneMenu,oneMenuVeiculo" />
</p:selectOneMenu>
[/code][/quote]
Entao, utilizei esse converter ele ta funcionando parcialmente… so q o nome do item ta aparecendo desse jeito:
requisicao.item.Item@7526ca33
Na verdade, eh o objeto item ai… Teria q mostrar o nome do item… Tenho q fazer alguma modificacao no converter…
@FacesConverter(value = "itemConverter")
public class ItemConverter implements Converter {
@Override
public Object getAsObject(FacesContext ctx, UIComponent component,
String value) {
if (value != null) {
return component.getAttributes().get(value);
}
return null;
}
@Override
public String getAsString(FacesContext ctx, UIComponent component,
Object obj) {
if (obj != null && !"".equals(obj)) {
String id;
try {
id = this.getId(getClazz(ctx, component), obj);
if (id == null) {
id = "";
}
id = id.trim();
component.getAttributes().put(id,
getClazz(ctx, component).cast(obj));
return id;
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassCastException e) {
e.printStackTrace();
}
}
return null;
}
private Class<?> getClazz(FacesContext facesContext, UIComponent component) {
return component.getValueExpression("value").getType(
facesContext.getELContext());
}
public String getId(Class<?> clazz, Object obj) throws SecurityException,
NoSuchFieldException, IllegalArgumentException,
IllegalAccessException {
for (Field field : clazz.getDeclaredFields()) {
if ((field.getAnnotation(Id.class)) != null) {
Field privateField = clazz.getDeclaredField(field.getName());
privateField.setAccessible(true);
if (privateField.get(clazz.cast(obj)) != null) {
return (String) field.getType()
.cast(privateField.get(clazz.cast(obj))).toString();
} else {
return null;
}
}
}
return null;
}
}
<br/>
<fieldset><legend>Requisição</legend>
<h:panelGrid columns="3">
<h:outputText value="Item:"/>
<h:selectOneMenu id="itemSelecionado" value="#{carrinhoBean.produto.objetoItem}" converter="itemConverter" items="#{itemBean.itens}">
<f:selectItem itemLabel="Selecione" itemValue=""/>
<f:selectItems
value="#{itemBean.itens}"
var ="itens"
itemValue="#{itens}"
itemLabel="#{itens.nome}"/>
</h:selectOneMenu>
[/quote]
Resolvido…
So foi adicionar .nome no atributo objetoItem… Muito obrigado a todos pela ajuda.
<f:facet name="header">Nome do Item</f:facet>
<h:outputText value="#{car.objetoItem.nome}" />