2

I got a problem using selectOneMenu, it can clearly convert the Suppliers to SupplierBean (my boss use to call it that way-he was the one who set it up), and display it correctly on the page, but the moment i save it, it returns a null value.

My code in 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>

Code in 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;
}
}

Code for converter:

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

Overview of the methods in my backingBean that i use to save the supplier(I used to call it controller):

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.");
}
Paul Deldacan
  • 111
  • 3
  • 6
  • 1
    Please elaborate the vague and overly generic statement *"it returns a null value."* in more detail. Which variable exactly are you talking about? `id`? `manager`? `sb`? Where exactly in the code are you observing this? Before `getAsObject()`? Inside `getAsObject()`? After `getAsObject()`? etc. – BalusC Apr 23 '13 at 11:52
  • I'm experiencing the same thing. I think its a bug. The getAsString runs on the converter for me. After applying request values, it's still null. The string value in the on the HTML element has been being converted when the page renders correctly. – anger Aug 23 '13 at 14:13

0 Answers0