Search code examples
javaprimefacesjsf-2

getAsObject string value is the word null


I have the following selectOneMenu :

<p:outputLabel value="Transaction Type:"/>
<p:selectOneMenu value="#{accountFormBean.account.transType}" converter="#{transTypeConverter}" onchange="changedValue(this)" filter="true" filterMatchMode="startsWith">
    <f:selectItems value="#{accountFormBean.transTypeList}" var="tType" itemValue="#{tType}" itemLabel="#{tType.transTypeName}" />
    <f:selectItem itemLabel="Add Transaction Type" itemValue="#{accountFormBean.dialogSelectTransType}"/>
</p:selectOneMenu>

The selectItem for "Add Transaction Type" isimply opens a dialog when clicked, if left selected and the form submitted it will send "-1" to the converter. The TransTypeConverter getAsObject method is as follows :

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
    TransType dummy = new TransType();
    dummy.setTransTypeId(-1L);

    if(value.equals("-1")) {
        return dummy;
    } else if (value.equalsIgnoreCase("null")) {
        return null;
    } else {
        return transTypeManager.getTransType(Long.parseLong(value));
    }
}

My problem is that getAsObject is called twice when I select something on the list, first with the string id and then with a string with the value of the word null(ie. String s = "null"). This is of course not difficult to check for and handle, but I am more interested in why this happening. The javadocs for getAsObject only says the value can be null, not "null". I also have converters in other projects that do not have this problem and I can't see any difference that would cause it.

Edit : changedValue() function :

function changedValue(element){
    var transVal = $(element).find('option:selected').text();
    if(transVal === "Add Merchant"){
        PF('merchantDialog').show();
    }

    if(transVal === "Add Transaction Type"){
        PF('transTypeDialog').show();
    }

    if(transVal === "Add Payment Type"){
        PF('paymentTypeDialog').show();
    }

}

This is running on Java 1.6, Tomcat 7, JSF 2.1, Primefaces 5.0, though it has also run on Java 1.8 with the same problem.


Solution

  • Ok so I found the reason for the second method call.

    On this same page I have 3 dialogs that are hidden by default. One of them also has a selectOneMenu which uses the same converter, but it isn't populating correctly thus if this dialog isn't used the value it defaults to is "null". Of course on form submission even though the value isn't used it still has to go through the converter and that is where the second method call comes from.

    For reference the selectOneMenu in question :

    <p:selectOneMenu id="transTypeDiaId" value="#{merchantFormBean.merchant.transType}" onchange="changedValue(this)" converter="#{transTypeConverter}" filter="true" filterMatchMode="startsWith">
        <f:selectItems value="#{merchantFormBean.dropdownTransTypes}" var="type" itemLabel="#{type.transTypeName}" itemValue="#{type}" />
        <f:selectItem itemLabel="Add Transaction Type" itemValue="#{merchantFormBean.dialogSelect}"/>
    </p:selectOneMenu>