Search code examples
javaajaxjsfjakarta-eerichfaces

Ajax and Request Scoped beans in JSF 1.2 and Rich Faces


I'm using JSF 1.2 and RichFaces 3.3, I have a page called NewsItemDetails with the following structure

<h:form>
  <h:panelBar>
    <h:panelBarItem>
      <myTag:searchModule />
    </h:panelBarItem>
  </h:panelBar>
</h:form>

and the backing bean of that page is of a request scope, and here is a snippet of the faces-config.xml:

<managed-bean>
  <managed-bean-name>newsItemDetails</managed-bean-name>
  <managed-bean-class>class.location...</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
  <managed-property>
     <property-name>id</property-name>
     <value>#{param['id']}</value>
  </managed-property>
</managed-bean>

And to be complete here's a snippet of the searchModule tag:

<ui:composition>
   <rich:tabPanel>
     <rich:tab label="Images">
       <h:panelGrid columns="2">
         <h:inputText value="#{newsItemDetails.searchLabel}" />
         <a4j:commandButton value="Search" actionListener="#{newsItemDetails.onSearch}" reRender="mediaItemsSearchResults, imagesDataTable" />
       </h:panelGrid>

       <a4j:outputPanel id="mediaItemsSearchResults" ajaxRendered="true">
         <rich:dataTable id="imagesDataTable" value="#{newsItemDetails.searchResults}" var="image">
           <h:column>
             <h:outputText value="#{image.title}"/>
           </h:column>
         </rich:dataTable>
       </a4j:outputPanel>

     </rich:tab>
   </rich:tabPanel>
</ui:composition>

Now the problem is, whenever I hit the search button, the action listener is never called, but I could see a request sent to the server through Firebug. And here's the strange thing if I call the same searchModule tag, just outside the rich:panelBarItem and I hit the search button the action listener is called, but the dataTable is not re-rendered, and it's only called the first time I hit the button.

So if anyone has a clue why this is happening, I would be more than grateful.

Thanks in advance


Solution

  • I've figured out the answer, apparently the scope of the backing bean had to be set to session scope instead of request. Having done that everything works great. So the entry in the faces-config.xml is:

    <managed-bean>
      <managed-bean-name>newsItemDetails</managed-bean-name>
      <managed-bean-class>class.location...</managed-bean-class>
      <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
    

    And as you can see I had to remove the entry, and instead get it from the request object in the backing bean.

    Thanks all.