0

I could help me know how to get the selected object


xhtml:

<p:selectOneRadio id="selection" value="#{miCurso.respuestaDTO}">
    <f:selectItems value="#{pregunta.respuestas}" var="respuesta" itemLabel="#{respuesta.respuesta}"  itemValue="#{respuesta}" />                               
</p:selectOneRadio>

I want to get the whole object not just itemLabel

Michael Vega
  • 39
  • 1
  • 9

1 Answers1

0
<p:selectOneRadio id="selection" value="#{miCurso.respuestaDTO}" converter="clientesConverter">
<f:selectItems value="#{pregunta.respuestas}" var="respuesta" itemLabel="#{respuesta.respuesta}"  itemValue="#{respuesta}" />                               

coverter for selectoneradio:

package JSF_Converters;

@FacesConverter(value = "clientesConverter")
public class ClientesConverter implements Converter {

@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
    if (arg2 == null || arg2.equals("")) {
        return "";
    }
    if (arg0 == null) {
        throw new NullPointerException("context");
    }
    if (arg1 == null) {
        throw new NullPointerException("component");
    }
    return ((EmpresaClienteUtil) arg2).getCodigo();
}

@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
    if (arg2.trim().equals("")) {
        return null;
    }
    if (arg0 == null) {
        throw new NullPointerException("context");
    }
    if (arg1 == null) {
        throw new NullPointerException("component");
    }
    FacesContext ctx = FacesContext.getCurrentInstance();
    ValueExpression vex = ctx.getApplication().getExpressionFactory().createValueExpression(ctx.getELContext(), "#{comunMB}", ComunMB.class);
    ComunMB items = (ComunMB) vex.getValue(ctx.getELContext());
    EmpresaClienteUtil item;
    try {
        item = items.getItemClientes(arg2); 
    } catch (Exception e) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Valor desconocido", "El valor es desconocido!");
        throw new ConverterException(message + e.getMessage());
    }
    if (item == null) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Valor desconocido", "El valor es desconocido!");
        throw new ConverterException(message);
    }
    return item;
}

}

ComunMB

package ManagedBean;


@Named(value = "comunMB")
@ApplicationScoped
public class ComunMB {

public ComunMB() {
}

private HashMap<String, EmpresaClienteUtil> myHPClientes = new HashMap<String,EmpresaClienteUtil>();

public EmpresaClienteUtil getItemClientes(String clave) {
    return (EmpresaClienteUtil) myHPClientes.get(clave);
}

}

meyquel
  • 2,134
  • 5
  • 23
  • 42