2 Replies Latest reply on Jul 27, 2009 7:17 PM by Arbi Sookazian

    JSF action versus seam page action

    Tanya Ruttenberg Expert

      I have a search form with this button to submit the form (straight from seam-gen)


      <h:commandButton value="Search" action="#{searchListener.goSearch()}" />




      I also have an input on my page like this:


      <h:inputText id="devName" value="#{devices.devName}" />



      When I invoke this action, the devices object is created and devName is passed to searchListener.goSearch() though the value of hostname never does make it to the result page.


      I am trying to push navigation onto the page.xml file for this view because I need to navigate based on whether devices.devName has a value on it. 


      And as such I do this instead:


      <h:commandButton value="Search" action="/Result.xhtml" />



      And add an action to Result.page.xml along with the appropriate parameter kinda like this:




      <param name="hostname" value="#{devices.devName}"/>
      <action execute=searchListener.goSearch()">





      And some navigation here based on outcome of goSearch.
      In the latter example, the value of devices.devName does not get into goSearch.  A new devices object seems to be created instead.  How do I put an action into the page.xml and get it to use a parameter from that file?


      TDR

        • 1. Re: JSF action versus seam page action
          Shervin Asgari Master

          You should definetly don't do like that.


          Read the Seam pageflow


          Basically what you can do is have a method that returns a string, and based on that string you can navigate to whatever page you want. You write that in your page.xml file or pages.xml.


          For instances:




          <page view-id="/numberGuess.jsp">
              <navigation>
                  <rule if-outcome="guess">
                      <redirect view-id="/numberGuess.jsp"/>
                  </rule>
          
                  <rule if-outcome="win">
                      <redirect view-id="/win.jsp"/>
                  </rule>
          
                  <rule if-outcome="lose">
                      <redirect view-id="/lose.jsp"/>
                  </rule>
              </navigation>
          </page>
          
          





          public String guess() {
          
              if (guess==randomNumber) return "/win.jsp";
          
              if (++guessCount==maxGuesses) return "/lose.jsp";
          
              return null;
          
          }





          • 2. Re: JSF action versus seam page action
            Arbi Sookazian Master

            If you don't need to set a value in a Seam component via pages.xml (or via injection, for example), then the example above (returning /foo.xhtml from an action method) will work well strictly for navigation purposes. 


            But to answer your question: How do I put an action into the page.xml and get it to use a parameter from that file?


            You have a couple of options that I'm aware of.  One is using page params and the other is @RequestParameter to inject the value into your Seam component.


            An example I just looked at that works for the page param approach is in the seamdvd example project.


            pages.xml:


            <page view-id="/dvd.xhtml" 
                      action="#{search.selectFromRequest}"> 
            
                    <param name="id" value="#{search.selectedId}"/> 
            
                    <navigation from-action="#{search.addToCart}">
                        <redirect view-id="/dvd.xhtml"/>
                    </navigation>
                </page>



            I added a breakpoint here:


            @Stateful
            @Name("search")
            public class FullTextSearchAction
                implements FullTextSearch,
                           Serializable
            {...
               public void setSelectedId(Long id) {
                    this.id = id;   //this line is executed!
                }
            }



            key point: setter is called prior to action method execution.


            HTH