primefaces的selectOneMenu,转换器不能与我的backingBean一起使用

时间:2013-04-23 08:11:56

标签: jsf converter selectonemenu

我使用selectOneMenu时遇到了问题,它可以清楚地将供应商转换为SupplierBean(我的老板用这种方式调用它 - 他是设置它的人),并在页面上正确显示它,但是当下我保存它,它返回一个空值。

我在XHTML中的代码:

    <p:selectOneMenu value="#{itemSupplierController.supplierBean}"
converter="supplierConverter">
<f:selectItem itemLabel="Select..." itemValue="" />
<f:selectItems value="#{supplierController.suppliersBean}"
    var="s" itemValue="#{s}" itemLabel="#{s.supplierName}" />
</p:selectOneMenu>

SupplierBean中的代码:

public class SupplierBean {
private int id;
private String supplierName;
public SupplierBean(){
}

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getSupplierName() {
    return supplierName;
}
public void setSupplierName(String supplierName) {
    this.supplierName = supplierName;
}
}

转换器代码:

@FacesConverter(value = "supplierConverter")
public class SupplierConverter implements Converter {

private static final Logger logger = Logger.getLogger("SupplierConverter");

@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String id) {
    logger.info(id);
    SupplierManager manager = EjbInitializer.getSupplierManager();
    if (StringUtils.isNullOrEmpty(id)
            || !org.apache.commons.lang.math.NumberUtils.isNumber(id)) {
        return null;
    } else {
        SupplierBean sb = null;
        try {
            sb = convertToPojo((Supplier) manager.find(Integer.valueOf(id)));
        } catch (SoftTechPersistenceException e) {
            e.printStackTrace();
        }
        return sb;
    }
}

@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object s) {
    String val = null;
    logger.info("object: " + s);
    if (s != null && (s instanceof SupplierBean)) {
        SupplierBean supplier = (SupplierBean) s;
        val = Integer.toString(supplier.getId());
    }
    logger.info(String.format("value %s", val));
    return val;
}

public static SupplierBean convertToPojo(Supplier s) {
    SupplierBean supplier = new SupplierBean();
    supplier.setId(s.getId());
    String name = "";
    if (s.getFullName().isEmpty()) {
        name = s.getFullName();
    } else {
        name = s.getCompany();
    }
    supplier.setSupplierName(name);
    return supplier;
}
}

我的backingBean中用于保存供应商的方法概述(我以前称之为控制器):

public void supplierSave() {
    logger.info("supplier save or update commenced.");
    if (SupplierAction.Create.equals(supplierCurrentAction)) {
        logger.info("adding supplier to the table...");
        addSupplierToTable();
    } else if (SupplierAction.Update.equals(supplierCurrentAction)) {
        logger.info("supplier updating...");
        updateSupplier();
    }
}

public void addSupplierToTable() {
    try {
        logger.info(String.format("supplier id: %s", getSupplierBean().getId()));
        setSupplier((Supplier)supplierManager.find(getSupplierBean().getId()));
        getItemSupplier().setSupplier(getSupplier());
        getItemSuppliers().add(getItemSupplier());
        resetSupplier();
    } catch (Exception e) {
        e.printStackTrace();
    }
    logger.info("supplier successfully added to the table.");
}

0 个答案:

没有答案