Problem with p:selectOneMenu and converter.

UI Components for JSF
Post Reply
mauriciojdsantos
Posts: 2
Joined: 20 Apr 2012, 18:29

20 Apr 2012, 18:51

I am trying to implement a p:selectOneMenu that return the object is the list, in this case an AreaVisitacao.

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>
I've already looked for this but I have seen no problem like this.

Thanks.

Hamsterbau
Posts: 409
Joined: 28 Dec 2011, 17:44

20 Apr 2012, 18:55

Constructs with value, var, itemLabel and ItemValue need no converter. Just remove it and change your itemValue to the real object:

Code: Select all

itemValue="#{t}"
I like JSF2, in JSF 1.x you need for these selectOneMenu-tags converters all the time ...
Primefaces 8.0.7 (PF Extensions 8.0)
JSF: Mojarra 2.3.2 (Spec 2.3) - running on WildFly 22

mauriciojdsantos
Posts: 2
Joined: 20 Apr 2012, 18:29

24 Apr 2012, 20:51

Thanks!

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 43 guests