1

I filled my rightList of the PickList with Objects from a web service, but when i select some elements, i want to get these elements in myManagedBean, because i'll affect them to an Object.

JSF :

            <h:form>
                <rich:panel>
                    <h:panelGrid columns="2" styleClass="criteresSaisie"
                        rowClasses="critereLigne" columnClasses="titreColonne,">

                        <h:outputLabel for="libelleCoplement" value="           "
                            size="20" />
                        <rich:pickList d="libelleCoplement" sourceCaption="Compléments"
                            targetCaption="Compléments sélectionnés"
                            value="#{listeCmpltDispoModel.listeCmpltSelect}" size="15"
                            addText="&gt;" addAllText="&gt;&gt;" removeText="&lt;"
                            removeAllText="&lt;&lt;" listWidth="270px" listHeight="110px"
                            orderable="true">
                            <f:converter converterId="cmpltsTitresConcerter" />
                            <f:selectItems value="#{listeCmpltDispoModel.listeCmpltDispo}"
                                var="liste" itemLabel="#{liste.libelleComplement}"
                                itemValue="#{liste.cdComplement}"/>

                        </rich:pickList>
                    </h:panelGrid>

                    <h:panelGroup>
                        <div align="right">
                            <h:panelGrid columns="8">
                                <h:commandButton value="Valider"
                                    action="#{saisieCmpltsTitreCtrl.valider}" />
                            </h:panelGrid>
                        </div>
                    </h:panelGroup>

                </rich:panel>
            </h:form>

The bean :

@ManagedBean(name = "listeCmpltDispoModel")
@SessionScoped
public class ListeCmpltDispoModel implements Serializable {
private static final long serialVersionUID = 1L;

private Long cdComplement;
private String libelleComplement;
private int nbCompl;

private List<ComplementsDispoSortieDTO> listeCmpltDispo ;
private List<ComplementsDispoSortieDTO> listeCmpltSelect ;

public ListeCmpltDispoModel() {
}

public Long getCodeComplement() {
    return cdComplement;
}

public void setCodeComplement(Long cdComplement) {
    this.cdComplement = cdComplement;
}

public String getLibelleComplement1() {
    return libelleComplement;
}

public void setLibelleComplement1(String libelleCoplement) {
    this.libelleComplement = libelleCoplement;
}

public Long getCdComplement() {
    return cdComplement;
}

public void setCdComplement(Long cdComplement) {
    this.cdComplement = cdComplement;
}


public String getLibelleComplement() {
    return libelleComplement;
}

public void setLibelleComplement(String libelleComplement) {
    this.libelleComplement = libelleComplement;
}

public List<ComplementsDispoSortieDTO> getListeCmpltDispo() {
    return listeCmpltDispo;
}

public void setListeCmpltDispo(List<ComplementsDispoSortieDTO> listeCmpltDispo) {
    this.listeCmpltDispo = listeCmpltDispo;
}

public int getNbCompl() {
    return nbCompl;
}

public void setNbCompl(int nbCompl) {
    this.nbCompl = nbCompl;
}

public List<ComplementsDispoSortieDTO> getListeCmpltSelect() {
    return listeCmpltSelect;
}

public void setListeCmpltSelect(List<ComplementsDispoSortieDTO> listeCmpltSelect) {
    this.listeCmpltSelect = listeCmpltSelect;
}
}

Converter :

@FacesConverter(value="cmpltsTitresConcerter")
public class CmpltsTitresConcerter implements Converter {

@SuppressWarnings("null")
public Object getAsObject(FacesContext context, UIComponent component,
        String value){
    ComplementsDispoSortieDTO cmpltSelect = null;
    cmpltSelect.setCdComplement(Long.parseLong(value));
    return cmpltSelect;
}

public String getAsString(FacesContext arg0, UIComponent arg1, Object obj) {
    return String.valueOf(((ComplementsDispoSortieDTO) obj).getCdComplement());
}
 }

Any help is greatly apprectiated!

Netmaster
  • 287
  • 1
  • 10
  • 27
  • You put a few elements in the right column and you want to select some from those few? Why don't you put there only the elements that you need? – Makhiel May 21 '13 at 13:39
  • I want to only just the elements in the right side into an Object, I get only the code id! – Netmaster May 21 '13 at 13:45

2 Answers2

1

Roughtly you need 3 things :

  • Custom converter for your object (Object to String, String to Object)
  • Getter/Setter with the List of your Objects choices
  • Getter/Setter with the List of your Objects selected

Everything is perfectly described here : RichFaces Showcase - pickList

EDIT :

Adding this should fix your problem :

<rich:pickList ...>
    <f:converter converterId="cmpltsTitresConcerter" />
</rich:pickList>

also the converter property in <f:selectItems /> is not valid : f:selectItems

EDIT :

You should modify your converter like that to remove converting exceptions :

@FacesConverter(value="cmpltsTitresConcerter")
public class CmpltsTitresConcerter implements Converter
{
    public Object getAsObject(FacesContext context, UIComponent component, String value)
    {
        ComplementsDispoSortieDTO cmpltSelect = null;

        if(value != null)
        {
            cmpltSelect = new ComplementsDispoSortieDTO();
            cmpltSelect.setCdComplement(Long.parseLong(value));
        }

        return cmpltSelect;
    }

    public String getAsString(FacesContext arg0, UIComponent arg1, Object obj)
    {
        String result = null;

        if(obj != null)
        {
            result = String.valueOf(((ComplementsDispoSortieDTO) obj).getCdComplement());
        }

        return result;
    }
}
Alexandre Lavoie
  • 8,711
  • 3
  • 31
  • 72
0

Your selected objects are bound to the value attribute which must have a getter and setter.

Christophe Roussy
  • 16,299
  • 4
  • 85
  • 85
  • Yes I have getter and setter in my object, but I got a string value (memory case) or I should select one attribute, but I want to get the object – Netmaster May 22 '13 at 12:26