3 Replies Latest reply on May 6, 2009 7:31 PM by Tanya Ruttenberg

    How to set a default parameter value

    Tanya Ruttenberg Expert
      My main view is a basic search form called basicSearch.xhtml.  When you click put some input and click Search it jumps to officeList view with all the fields populated. In other words it basically works.  It has 5 possible inputs and here is the basicSearch.page.xml file:

         <param name="ring" value="#{officeViewList.officeView.ring}"/>
         <param name="siteCode" value="#{officeViewList.officeView.siteCode}"/>
         <param name="state" value="#{officeViewList.officeView.state}"/>
         <param name="type" value="#{officeViewList.officeView.type}"/>
         <param name="devName" value="#{devicesList.devices.devName}" />

      The entity which populates officeList is OfficeView.  The primary key/id for OfficeView is office_id/officeID (param name/entity propert).

      I want to be able to access the officeList view with just the office_id parameter set.  Like this

      http://localhost:8080/ond/officeList.seam?office_id=7872

      This works as well.  In order for this to work, I had to include office_id in officeList.page.xml like this:

         <param name="office_id" value="#{officeViewList.officeView.officeId}"  />

      etc. the rest of the parameters are not included here for brevity.

      The problem is that when office_id is set as a parameter in officeList.page.xml, it turns up with a value of "0" when I click through to the officeList view from the basicSearch view.  For instance to display offices in Alaska, I input AK into basicSearch and end up with this URL

      http://localhost:8080/ond/officeList.seam?ring=&state=AK&devName=&type=&siteCode=&office_id=0cid=4

      rather than this one

      http://localhost:8080/ond/officeList.seam?ring=&state=AK&devName=&type=&siteCode=&office_id=cid=4

      Of course I get zero results because there is no office with an office_id of 0 and a state of AK.

      And yet I need that office_id parameter to be able to use this handy URL to access a single office, as I mention above:

      http://localhost:8080/ond/officeList.seam?office_id=7872

      How do I make office_id set to "" rather than "0" on queries generated by submitting the form in basicSearch? NOthing else gets a default value. Is it because officeId is type int and all other properties are type String?

      How do I get around this?

      Perhaps related -- I have an link on the officeList view back to basicSearch and I want office_id to be unset when someone clicks that link. How can I do this?

      Here is the link as it is now:
      <s:link value="Basic Search" view="/basicSearch.xhtml" /><br />

      TDR
        • 1. Re: How to set a default parameter value
          Tanya Ruttenberg Expert

          I figured out either 1) the correct way to do this or 2)a workaround (depending on your perspective).


          To display a single OfficeView instance rather than a list, I should use the OfficeViewHome action bean rather than the OfficeViewList action bean with the primary key as a parameter.


          Hope my terminology is correct.  I'm still learning...


          TDR

          • 2. Re: How to set a default parameter value
            Arbi Sookazian Master

            Are you submitting an HTTP GET request to have a URL result like this:


            http://localhost:8080/ond/officeList.seam?ring=&state=AK&devName=&type=&siteCode=&office_id=cid=4



            Do you actually want your URL strings to look like that (e.g., be careful with passing sensitive data like that)?


            I would use this approach as per DAllen:


            The @RequestParameter annotation is used to inject an HTTP request parameter into
            the property of a component, retrieved either from the query string or the form data.



            For example, if you notice this web site's URL pattern, it never displays any HTTP request params in the URL other than cid.


            As per Seam ref doc:


            @RequestParameter
            @RequestParameter("parameterName")
            Specifies that a component attribute is to be injected with the value of a request parameter.
            Basic type conversions are performed automatically.
            • value — specifies the name of the request parameter. Default to the name of the
            component attribute.



            I'm pretty sure you can use @RequestParameter to inject HTTP POST or HTTP GET params, but don't quote me on that.


            So in your specific case, you can refactor the following code from pages.xml:


            <param name="office_id" value="#{officeViewList.officeView.officeId}" />



            into your JSF/xhtml:


            <f:param name="office_id" value="#{officeViewList.officeView.officeId}" />



            Core JSF:


            If you attach f:param to a command component such as a button or link, the JSF implementation passes the parameter's value to the server as a request parameter.

            • 3. Re: How to set a default parameter value
              Tanya Ruttenberg Expert

              I tried <f:param>, but I think I used it incorrectly because it didn't work.

              Re: @RequestParameter.  That is something to consider.


              I don't think I mind some info in the URL. None of it is sensitive. However, especially with another search page (an advanced search) it could get a bit unwieldy.  I'll have to think about that.  Probably some would come in as URL's and some via RequestParameters.


              As for whether I'm using a POST or GET, I don't know if I'm using an HTTP GET. I'm used modified CRUD form from seam-gen.


              I think using a separate view for this that access the OfficeViewHome entity is the way to go. After I posted this, I realized that I also had added office_id as a SELECT criteria and that really seemed to mess things up.  My "AK" URL (as listed above) no longer worked until I removed office_id from the SELECT criteria RESTRICTION list.  It must be because office_id is the entity's ID that it was showing up in the query no matter what else was being passed to it. Even when I removed all reference to it from .page.xml, as long as it stayed in the query it kept turning up as office_id=0. In other words no queries worked!


              Thank you for the help. Every time I post/read this list I learn something new.


              TDR