1

i'm trying to use an search button which brings back the selected items (can be more than one) and it updates the datatable. Then i have a selectBooleanCheckbox next to each colomn, when user selects "n" items then presses the Select the checked Items it insert DB.

The code can be seen below:

<h:panelGrid columns="5">
    <h:form>
      <h:outputText value="Item Name"/>
      <p:inputText value="#{StockController.itemName}"/>
      <h:commandButton value="Search" action="#{StockController.Search}">
         <f:ajax execute="@form" render=":results"/>
      </h:commandButton>
    </h:form>

//The code sample below belongs to BalusC see the post here

    <h:panelGroup id="results">
    <h:form>
    <h:dataTable value="#{bean.entities}" var="entity">
        <h:column>
            <h:selectBooleanCheckbox value="#{bean.checked[entity.id]}" />
        </h:column>
        ...
    </h:dataTable>
    <h:commandButton value="Select the checked Items" action="#{StockController.insertDao}" >
  <f:ajax execute="@form" render=":results"/>
 </h:commandButton>
 </h:form>
</h:panelGrid>

Now, i have read many blogs and Core javaServer Faces 3, i don't think there is logical error in ajax usage. I tested by removing each ajax then both works fine but whenever i try to use both of cummondButtons with ajax, the second one "Select the checked Items" does not even call the "StockController.insertDao" method.

Any help is appreciated.

Thanks all.

Community
  • 1
  • 1
berkay
  • 3,907
  • 4
  • 36
  • 51

1 Answers1

3

When you want to ajax-render content which in turn contains another form, then you need to include the ID of that form in the render attribute as well, otherwise the other form will lose its view state and nothing will be processed on submit.

<h:form>
  ...
  <h:commandButton value="Search" action="#{StockController.Search}">
    <f:ajax execute="@form" render=":results :resultsForm" />
  </h:commandButton>
</h:form>

<h:panelGroup id="results">
  <h:form id="resultsForm">
    ...
  </h:form>
</h:panelGroup>

If there's however nothing which goes outside <h:form> inside the same <h:panelGroup>, then you can just omit the <h:panelGroup> altogether and re-render the form directly.

<h:form>
  ...
  <h:commandButton value="Search" action="#{StockController.Search}">
    <f:ajax execute="@form" render=":results" />
  </h:commandButton>
</h:form>

<h:form id="results">
  ...
</h:form>

This is related to JSF spec issue 790.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • thank you very much just start learning jsf and make this kind of errors easily. Now i got it. i also want to say that i'm a big fan of your blog and responses. – berkay Feb 01 '12 at 13:05
  • You're welcome. I wouldn't immediately say that this one is your own mistake. This is not intuitive. It's just a bug/oversight in the JSF spec which needs to be workarounded until they fix it. – BalusC Feb 01 '12 at 13:06