Note that there are some explanatory texts on larger screens.

plurals
  1. POprimefaces' selectOneMenu with converter not working with my backingBean
    text
    copied!<p>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.</p> <p>My code in XHTML:</p> <pre><code> &lt;p:selectOneMenu value="#{itemSupplierController.supplierBean}" converter="supplierConverter"&gt; &lt;f:selectItem itemLabel="Select..." itemValue="" /&gt; &lt;f:selectItems value="#{supplierController.suppliersBean}" var="s" itemValue="#{s}" itemLabel="#{s.supplierName}" /&gt; &lt;/p:selectOneMenu&gt; </code></pre> <p>Code in SupplierBean:</p> <pre><code>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></pre> <p>Code for converter:</p> <pre><code>@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 &amp;&amp; (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; } } </code></pre> <p>Overview of the methods in my backingBean that i use to save the supplier(I used to call it controller):</p> <pre><code>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."); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload