2

我在使用 selectOneMenu 时遇到了问题,它可以清楚地将 Suppliers 转换为 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.");
}
4

0 回答 0