0

I have one p:selectManyMenu where I can choose multiple items, but I can't manage to get that values, it is always null so I'm not sure if I'm making mistake in processing components or my converter isn't good. Here is XHTML:

<h:form id="form">              
           <p:selectManyMenu id="advanced" value="#{pickListView.chosenItems}" converter="converterTest"
                          var="t" filter="true" filterMatchMode="contains" showCheckbox="true" >
                <f:selectItems value="#{pickListView.allEquipment}" var="record" itemLabel="#{record.name}" itemValue="#{record}"/>

                <p:column style="width:90%">
                    <h:outputText styleClass="f_text" value="#{t.name} - #{t.price}" />
                </p:column>

            </p:selectManyMenu>

            <p:commandButton id="pojoSubmit" value="Spremi" oncomplete="PF('dlg').show()" actionListener="#{pickListView.saveRecords}" update=":form:table-wrapper" style="margin-top:5px" process="@this" />

                <p:dialog header="Selected Values" modal="true" showEffect="fade" widgetVar="dlg" resizable="false">
                <p:panelGrid columns="1" id="display" columnClasses="label,output">

                    <p:dataList value="#{pickListView.chosenItems}" var="t">
                        <h:outputText value="#{t}" />
                    </p:dataList>
                </p:panelGrid>
            </p:dialog>

        </h:form>

So, my List<Equipment> chosenItems should contain selected items but is always null. I don't know what should I've done wrong, maybe it's up to converter not managing to return asObject, but I print data from converted object before return, as you can see below, and it's ok, so I'm very confused now...

Here is my Converter:

@FacesConverter(value = "converterTest")
public class ConverterTest implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {

        if(value != null && value.trim().length() > 0) {
            try {

                SessionFactory factory = HibernateUtil.getSessionFactory();
                Session session = factory.openSession();    
                Equipment o = (Equipment) session.get(Equipment.class, Integer.valueOf(value));
                System.out.println("EQUIPMENT: " + o.getName() + " : " + o.getId());

                return (Equipment) session.get(Equipment.class, Integer.valueOf(value));

            } catch(NumberFormatException e) {
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid equipment."));

            }
        }
        else {
            return null;
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object object) {

        return object instanceof Equipment ? 
                ((Equipment) object).getId().toString() : "";       
    }   
}

In bean I created two lists, one for all items and one for selected items with their getters and setters. Problem is that chosenItems list is always null.

private List<Equipment> chosenItems;
private List<Equipment> allEquipment;

//getters and setters

Any thoughts? If you need more details, feel free to ask. Thank you in advance!

Marko P
  • 89
  • 1
  • 12
  • Didn't you get a "Validation Error: Value is not valid" in a `` component or server log? – BalusC Jul 09 '15 at 20:30
  • No, there isn't any error, when I press "Submit" there is message inside p:dialog like "No record", and in saveRecords() method I can't read chosenItems list because it's null. It seems like converter do the job correctly, but selected items are missing :/ – Marko P Jul 09 '15 at 20:42
  • Does `Equipment` have `equals()` and `hashCode()` properly implemented? – BalusC Jul 09 '15 at 20:47
  • Well, Equipment is entity class generated by Hibernate. It only has property from database table (columns) with its getters and setters, no other methods. Can you suggest me what to do, it took me whole day and still without success? Is it Equipment class where should I implement equals() and hashCode()? – Marko P Jul 09 '15 at 20:57
  • Yes. Hibernate can be configured to add them during generation. The average IDE can generate it, too. Quick example: `public boolean equals(Object object) { return (object instanceof Equipment) && (id != null) ? id.equals(((Equipment) object).id) : (object == this); }` If that works, then this is technically a dupe: http://stackoverflow.com/questions/9069379/validation-error-value-is-not-valid (it's only strange that you didn't got this error) – BalusC Jul 09 '15 at 21:00
  • Thanks BalusC for your effort. Yes, I've generated it in Eclipse but without changes. I have similar symptoms as user on that link (converter doesn't return null, setter of chosenItems is never called), but this didn't help.. I worked by guidance from http://www.primefaces.org/showcase/ui/input/manyMenu.xhtml – Marko P Jul 09 '15 at 21:24
  • Bah, due to wide code snippet with a horizontal scrollbar I didn't immediately see you had a `process="@this"` on command button itself. Guess what it does. – BalusC Jul 09 '15 at 21:27

0 Answers0