在Primefaces SelectOneMenu中传递对象值

时间:2014-01-09 06:01:51

标签: jsf primefaces

您好我正在使用PrimeFaces 4.0,我需要在SelectOneMenu中传递对象值。 我正在使用转换器将其从字符串格式转换为Class对象格式。 这些是代码文件,请帮助我......

lablevalue.xhtml

<html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
    </h:head>
    <h:body>
        <h:form id="myform"> 
            <p:growl showDetail="true"></p:growl>
            <p:selectOneMenu value="#{itemlableAcction.idCard}" >
                <f:converter converterId="converter.SelectMenUConverter" />
                <f:selectItem itemLabel="Select" itemValue="" />
                <f:selectItems value="#{itemlableAcction.idCards}" var="idv" itemLabel="#{idv.name}" itemValue="#{idv}" />
            </p:selectOneMenu>
            <h:commandButton action="#{itemlableAcction.onclickSubmit}" value="Submit"></h:commandButton>
        </h:form>
    </h:body>
</html>

ItemlableAcction

import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import bo.IdCard;

@ManagedBean
public class ItemlableAcction {

    List<IdCard> idCards = new ArrayList<IdCard>();

    IdCard idCard;

    public Object getIdCard() {
        return idCard;
    }

    public void setIdCard(IdCard idCard) {
        this.idCard = idCard;
    }

    public List<IdCard> getIdCards() {
        return idCards;
    }

    public void setIdCards(List<IdCard> idCards) {
        this.idCards = idCards;
    }

    public ItemlableAcction() {
        IdCard card1 = new IdCard();
        card1.setId(1);
        card1.setName("ABC");
        card1.setAddress("USA");
        idCards.add(card1);
        IdCard card2 = new IdCard();
        card2.setId(2);
        card2.setName("MNO");
        card2.setAddress("INDIA");
        idCards.add(card2);
        IdCard card3 = new IdCard();
        card3.setId(3);
        card3.setName("XYZ");
        card3.setAddress("Chaina");
        idCards.add(card3);
    }

    public String onclickSubmit() {
        IdCard ic = (IdCard) idCard;
        System.out.println("In action id values are " + ic.getId() + " " + ic.getAddress());
        return "";
    }

}

SelectMenUConverter

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import bo.IdCard;

@FacesConverter("converter.SelectMenUConverter")
public class SelectMenUConverter implements Converter {

    public SelectMenUConverter() {
        System.out.println("Inside converter");
    }

    public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
        System.out.println("One" + arg2);
        IdCard idCard = new IdCard(arg2);
        return idCard;
    }

    public String getAsString(FacesContext arg0, UIComponent arg1, Object value) {
        System.out.println("Two" + value);
        return value.toString();
    }

}

Idcard

public class IdCard {

    String name;

    int id;

    String address;

    public IdCard() {
    }

    public IdCard(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

}

2 个答案:

答案 0 :(得分:3)

想象一下,您必须为100个类实现转换器。

如果您不想实现自己的Converter并从退出列表中获取数据,请使用:

SelectItems Converter Omnifaces

你有一个完整的例子。

PD:不要忘记使用唯一ID实现toString。(参见文档)

答案 1 :(得分:2)

通过getAsObject()的方法获取其标识时,您不会从定义的列表(在您的托管bean中)中搜索并获取所需的实例。您只是通过转换器实现了一个额外的新对象。试试这个:

@FacesConverter("converter.SelectMenUConverter")
public class SelectMenUConverter implements Converter {    
...

@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {

    if (arg2 == null || arg2.isEmpty()) {return null;}
    try {             
         return findIdCard(arg2);  // here's where should be retreived the desired selected instance
    } catch (NumberFormatException e) {
        throw new ConverterException(new FacesMessage(("This is not a valid card id")), e);
    }         
}

...

public IdCard findIdCard(String id) {
    Iterator<IdCard> iterator = idCards.iterator();   // "idCards" represents your idCards' list. It is not recognized yet in the converter
    while(iterator.hasNext()) {
        IdCard idc = iterator.next();   
        if(idc.getId() == Integer.valueOf(id).intValue()) {
            return idc;
        }
    }
    return null;
}