2 Replies Latest reply on Apr 26, 2007 12:00 PM by Fernando Montaño

    AJAX4JSF

    John Chesher Newbie

      I reused the AJAX4JSF search page/code from the booking example to build a search page for my application. I decided to make the search EJB session-scoped, so that my search criteria would still be there anytime I came back to the screen. I also wanted to display the "previous" search results whenever I came back to that page, but I wanted to perform a new search, just in case the results would be different searching with the same criteria, but at this new point in time. So, I added a page action in pages.xml. Everything seemed to work fine, but I noticed that whenever I do an AJAX-based search from the screen, it does the search twice, once for the AJAX call and apparently once again for the page action defined in pages.xml. I hadn't thought about it, but a coworker suggested that was behavior he was not expecting, as he thought the AJAX call would be a get rather than a post and that, since the entire page is not rerendered (we tested and it's not), Seam would not execute the page action specified in pages.xml.

      I implemented a "workaround" by having my AJAX call invoke a dummy method (does almost nothing), then letting the method specified on the page action do the real work of the search. This seems to provide the functionality I want without doing a redundant search when doing the AJAX-driven search, but we felt this was a pretty odd way to have to implement this functionality. Code snippets are below.

      Am I missing something? Is there a more "correct"/elegant way to implement the desired functionality when using Seam and AJAX4JSF together? The less-than-elegant solution I have now works, but I want to ensure I haven't leveraged unintended functionality, which could yield unexpected results or could be disallowed in a future release, or something like that.
      Thanks!

      Pages.xml:

      <page view-id="/secure/institution_search.xhtml" action="#{institutionSearch.prepSearchPage}">
       <navigation from-action="#{institutionSearch.selectInstitution}">
       <rule if-outcome="success">
       <redirect view-id="/secure/gen_stu_fac.xhtml"/>
       </rule>
       </navigation>
       </page>


      from the view:
      <h:inputText id="searchString" value="#{institutionSearch.searchString}" size="10">
       <a:support event="onkeyup" actionListener="#{institutionSearch.psuedoFindByAllCriteria}" reRender="searchResults" />
      </h:inputText>


      and the session bean:
      ...
      ...
      @Stateful
      @Synchronized(timeout=5000)
      @Name("institutionSearch")
      @Scope(ScopeType.SESSION)
      @Restrict("#{identity.loggedIn}")
      public class InstitutionSearchAction implements InstitutionSearch
      ...
      ...
      
      {
      
       public void prepSearchPage() {
       if ( ! user.getInstitutions().isEmpty()) {
       foundInstitutions = user.getInstitutionList(); //a case where we don't want to do an actual search...
       }
       else {
       if (prevSearchById) {
       findById(); //where real search occurs...
       }
       else {
       findByAllCriteria(); //where real search occurs...
       }
       }
       }
      
      
       public void psuedoFindByAllCriteria()
       {
       // Set to this value, so that the proper search will be executed when the page is rerendered
       prevSearchById = false;
       }
      ...
      ...