3 Replies Latest reply on Oct 22, 2009 5:29 PM by niox.nikospara.yahoo.com

    Understanding s:button h:commandButton and @RequestParameter

    daxxy

      Just to further my own understand of the difference between s:button and h:commandButton I am trying a little exercise.


      I have a simple form into which I input a String




      <h:inputText id="officeId" value="#{officeViewHome.officeViewOfficeId}"/>





      I submit the form with h:commandButton




      <h:commandButton label="Find Office" action="/OfficeView.xhtml"/>





      OfficeView.xhtml just displays some info about the office whose ID was entered in the form.


      This information about the OfficeViewHome instance in OfficeView.xhtml displays without a problem.



      I am attempting to see what other changes would be necessary were I to change the h:commandLink to s:button.  So here is what I did:


      Left the inputText field the same.


      Changed h:commandButton to this




      <s:button view="/OfficeView.xhtml" value="Find Office" >
       <f:param name="officeViewOfficeId" value="#{officeViewHome.officeViewOfficeId}"/>
      </s:button>





      Added this to OfficeViewHome.java




      @RequestParameter
      private Integer officeViewOfficeId;
      
      @Override public Object getId() {
        System.out.println("TDR getId "+ officeViewOfficeId);
            return officeViewOfficeId;
      }






      But I can see by my debugging statement that officeViewOfficeId is null.  It seems to me that the parameter is not getting passed to the @RequestParameter when I click on s:button.


      Can anyone see why this is not working and explain how to make it work?


      TDR

        • 1. Re: Understanding s:button h:commandButton and @RequestParameter
          niox.nikospara.yahoo.com

          Hi,


          This code:


          <s:button view="/OfficeView.xhtml" value="Find Office" >
           <f:param name="officeViewOfficeId" value="#{officeViewHome.officeViewOfficeId}"/>
          </s:button>
          



          Will issue a request using a parameter named officeViewOfficeId, whose value is the value of #{officeViewHome.officeViewOfficeId} when the page was rendered! If this value was null, you simply get the null back.


          The <s:button> does not submit the form, so any changes made at the client are discarded! I believe <s:button> is useful exactly for that, calling a server side action, triggering JSF navigation and propagating the conversation without submitting the form.

          • 2. Re: Understanding s:button h:commandButton and @RequestParameter
            daxxy

            I was just thinking about this some more and the light bulb was about to go off when I got your reply Nikos.  And that cinched it!


            I think I finally understand what it means that s:button does not submit the form. 


            You cannot use s:button to take information being entered into an input on that page and transfer it to another page.  You can only use it to transfer to another page information that already exists on the page when it is rendered. As in the ubiquitous example of a datatable with an Action column populated with links to Edit or Delete.


            Whether the information being transferred is newly entered on the page or exists already at page rendering is the difference between propagation and form submission.


            Thanks and I hope this posting helps other newbies.


            TDR

            • 3. Re: Understanding s:button h:commandButton and @RequestParameter
              niox.nikospara.yahoo.com

              Yes!