如何获取rich:pickList的targetList中的对象?

时间:2013-05-21 13:15:16

标签: jsf-2 richfaces

我用来自Web服务的对象填充了PickList的rightList,但是当我选择一些元素时,我想在myManagedBean中获取这些元素,因为我会将它们影响到一个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>

豆子:

@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;
}
}

转换器:

@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());
}
 }

任何帮助都非常感激!

2 个答案:

答案 0 :(得分:1)

你需要3件事:

  • 对象的自定义转换器(对象到字符串,字符串到对象)
  • 带有对象列表选择的Getter / Setter
  • 选择了对象列表的Getter / Setter

这里完美地描述了一切:RichFaces Showcase - pickList

编辑:

添加此项可以解决您的问题:

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

<f:selectItems />中的转换器属性也无效:f:selectItems

编辑:

您应该像这样修改转换器以删除转换例外:

@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;
    }
}

答案 1 :(得分:0)

您选择的对象绑定到value属性,该属性必须具有getter和setter。