1

I have file submission.xhtml with code snippet:

<p:outputLabel for="cbFilterPlanYear" value="Choose time period"/>

<p:outputPanel>
    <p:selectOneMenu id="cbFilterPlanMonth" value="#{SubmissionController.miFilterMonth}" style="width: 50%">
        <f:selectItem itemLabel="#{null}" itemValue="#{null}" />
        <f:selectItems value="#{SubmissionController.mlistMonth}" var="month" itemValue="#{month}" itemLabel="#{month}" />
        <p:ajax event="change" update="@this" />
    </p:selectOneMenu>

    <p:selectOneMenu id="cbFilterPlanYear" value="#{SubmissionController.miFilterYear}" style="width: 50%">
        <f:selectItem itemLabel="#{null}" itemValue="#{null}" />
        <f:selectItems value="#{SubmissionController.mlistYear}" var="year" itemValue="#{year}" itemLabel="#{year}" />
        <p:ajax event="change" update="@this" />
    </p:selectOneMenu>
</p:outputPanel>

These line of code for search criteria(s), for filter data. I can't submit NULL value (As you know, it also don't set specific criteria to search query). Help me submit null value from p:selectOneMenu.

enter image description here

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Vy Do
  • 46,709
  • 59
  • 215
  • 313
  • why you can't submit null value? if you no select any value in the select, your field in managedbean will be null – pedrohreis May 23 '16 at 02:30
  • It can't send `null` value. It keep old value, for example, if I choose May month before, it keep value `5`, don't change to `null`, when I submit. – Vy Do May 23 '16 at 02:32
  • really, see the @inafalcao's answer. i think `noSelectionOption="true"` fix your problem – pedrohreis May 23 '16 at 02:42
  • Please do not use [java] tag when asking JSF questions. – BalusC May 23 '16 at 07:32

2 Answers2

0

I think you could put just an empty string on your f:selectItem instead of itemLabel="#{null}" itemValue="#{null}" and add the attribute noSelectionOption="true"

<f:selectItem itemLabel="" itemValue="" noSelectionOption="true" />

Example here on primefaces docs.

inafalcao
  • 1,415
  • 1
  • 11
  • 25
  • has no problem in use `itemLabel="#{null}"` and `itemValue="#{null}"` But, I think that `noSelectionOption="true"` fix the problem – pedrohreis May 23 '16 at 02:41
  • I have no way to test this, but at least the empty string is less verbose, I think. :) – inafalcao May 23 '16 at 02:50
  • When I choose null value, It doesn't work, even it doesn't call method at controller when I press submit button. – Vy Do May 23 '16 at 02:53
  • If the value of the selectOneMenu is a string there's no difference. However, it can be a object of another class – pedrohreis May 23 '16 at 02:53
  • 1
    In either case, edited my answer to put this as an enhancement. – inafalcao May 23 '16 at 02:55
  • @dovy, paste the snippet of your submit button – pedrohreis May 23 '16 at 03:02
  • Instead of posting a "try this" / "I think" answer, please first try to reproduce the described problem yourself and test the potential solution yourself. If you're unable to, post a comment instead of an answer. At least, the `noSelectionOption` serves an entirely different purpose than what you're saying here: http://stackoverflow.com/questions/11360030/best-way-to-add-a-nothing-selected-option-to-a-selectonemenu-in-jsf – BalusC May 23 '16 at 07:31
-1

you can do like that :<f:selectItem itemValue="#{null}" itemLabel="" />

sawyinwaimon
  • 739
  • 1
  • 6
  • 14
  • When I choose `null` month or `null` year, even it doesn't call method `search(...)` at controller. – Vy Do May 23 '16 at 03:02
  • `public final void search() { try { mlistSubmission = msubmissionModel.search(strSearchSubmitNo, miFilterYear, strSearchSubmitType, mfilterOrg.getOrgCode(), miFilterMonth); } catch (Exception ex) { SystemLogger.getLogger().error(ex); ClientMessage.logErr(ClientMessage.MESSAGE_TYPE.ERR, ex.toString()); } }` – Vy Do May 23 '16 at 03:07