The converter isn't returning the object, it gives me an exception.
I have debugged and noticed that the problem is that in the attribute itemValue of f:selectItems the object that is in var is returning null but when i make a reference to this object in itemLabel it returns the correct value independent of the attribute.
So, if the itemValue is null, the value in the converter retrieves the itemLabel value, and booom, exception, because my method expects in value the id (that is referenced in itemValue) but it receives the name of the AreaVisitacao.
See the code:
Converter:
Code: Select all
package br.fiocruz.MB.conversores;
import javax.faces.component.UIComponent;
import javax.faces.component.UIParameter;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import br.fiocruz.dominio.AreaVisitacao;
import br.fiocruz.dominio.Usuario;
import br.fiocruz.dominio.dao.AreaVisitacaoDAO;
@FacesConverter(value="area-converter")
public class AreaVisitacaoConverter implements Converter {
@Override
public Object getAsObject(FacesContext arg0, UIComponent componente,
String valor) {
if (valor == null || valor.length() == 0)
return null;
AreaVisitacaoDAO dao = new AreaVisitacaoDAO();
AreaVisitacao area = dao.lerPorId(new Long(valor));
return area;
}
@Override
public String getAsString(FacesContext arg0, UIComponent componente,
Object objeto) {
if (objeto instanceof Usuario)
return ((AreaVisitacao) objeto).getId().toString();
return null;
}
}
The Component:
Code: Select all
<h:outputLabel class="formLabel" value="Area Responsável:" for="areaResponsavel"/>
<h:outputLabel class="formLabelRes" value="#{espacoFisicoMB.espacoFisico.areaResponsavel.nome}"
for="areaResponsavel" rendered="#{espacoFisicoMB.somenteLeitura}" />
<p:selectOneMenu id="areaResp" value="#{espacoFisicoMB.espacoFisico.areaResponsavel}"
size="1" converter="area-converter" rendered="#{not espacoFisicoMB.somenteLeitura}"
required="Selecione a área responsável" >
<f:selectItems value="#{espacoFisicoMB.possiveisAreasResp}" var="t" itemValue="#{t.id}"
itemLabel=" #{t.nome}" />
</p:selectOneMenu>
Thanks.