5 Replies Latest reply on Sep 4, 2008 11:26 PM by mail.micke

    Page parameters

    bashan

      Hi,


      I was having a little problem understand properly how to pass page parameters from the documents:


      http://docs.jboss.com/seam/2.0.1.GA/reference/en/html/events.html#d0e3899
      





      Is there some better article that clears things a bit better?


      Thanks.

        • 1. Re: Page parameters
          mail.micke

          Hi


          What is it that is unclear, how the parameters go from the URL into the backing bean?


          - micke

          • 2. Re: Page parameters
            bashan

            This is pretty much what I don't understand:
            - When does the "action" in pages.xml is activated: when navigating to this page only?, also on a posback? does Conversion scope behaves different then request scope? I mean, in converstion it is activated only once and in page scope it is activated always.
            - param property in pages.xml: If I use this param property, what does exactly the "value" means? injecting the request parameter to the bean property? If I use this parameter will I also have to use <s:link> with <f:param> on the originating url? or will it saves me from needing to use it?
            - On ajax request (ajax 4 jsf), will this parameter be also available to me?
            - On Seam request will this parameter be available to me?
            - Most of the time the parameter is only used to load an object. Where do I do the object loading? in the action?
            
            I know these questions are a bit stupid, but I believe I have these questions only because I used to work in my own (weird) way to overcome JSF parameters problem when I was using JSF only without Seam...
            
            Thanks
            (sorry for using code blocks, this editor is simply impossibe...)





            • 3. Re: Page parameters
              mail.micke


              • When does the action in pages.xml is activated: when navigating to this page only?, also on a posback? does Conversion scope behaves different then request scope? I mean, in converstion it is activated only once and in page scope it is activated always.




              • On ajax request (ajax 4 jsf), will this parameter be also available to me?



              I have an implementation which uses PAGE scope,it gets executed for each access to that page (even ajax requests).
              I get around this problem by having a boolean property on the backing bean and use that with the if atttribute (setting it to false after the first execute).


              Note: if you can use the @RequestParameter (setting the property of the backing bean, and not the property of a property) you can use the @Create annotation for executing code once only.(more on this further down).



              • param property in pages.xml: If I use this param property, what does exactly the value means? injecting the request parameter to the bean property? If I use this parameter will I also have to use <s:link> with <f:param> on the originating url? or will it saves me from needing to use it?



              Value is where the request parameter value specified by the name attribute should go.



              • On Seam request will this parameter be available to me?



              Not sure what you mean by a Seam request.



              • Most of the time the parameter is only used to load an object. Where do I do the object loading? in the action?



              Yes this is what I do, the action gets executed after the parameters have been set.


              One thing I noticed is that when using pages.xml the parameters gets set after @Create annotated methods are executed, so using an <action> element seems like  the only solution.
              Mentioning this because @RequestParameter annotated properties have their value set before the @Create method is executed.


              Hope this helped a little.


              Cheers,
              Micke

              • 4. Re: Page parameters
                bashan

                I wrote a long answer, but again, Seam editor raised error and I closed the window by mistake...  ;-)


                So, instead I will just write a problem I am having right now, and I hope you will be able to shade some light:
                I have a search box on the top of all my screens (it is in the main template). The search is in a form, and when pressing the search there is a search action. The search code looks like this:


                   <h:form id="formSearch">
                        <div style="float:left; margin-right:5px;">
                          <h:inputText value="#{searchAction.searchText}" style="width:200px;" />
                        </div>
                        <div style="float:left;">
                          <h:commandButton action="#{searchAction.search}" value="Find" />
                        </div>
                        </h:form>


                My searchAction bean looks like this:


                @Name("searchAction")
                public class SearchAction
                {
                  private String searchText;
                
                  public String getSearchText()
                  {
                    return searchText;
                  }
                
                  public void setSearchText(String searchText)
                  {
                    this.searchText = searchText;
                  }
                
                  public String search()
                  {
                    return "/search.xhtml?searchText=#{searchAction.searchText}";    
                  }
                }



                I am in one of my pages that needs a request parameter in order to be loaded. The page is loaded in this way:


                @Name("channelAction")
                public class ChannelAction
                {
                  @In
                  VideoDAO videoDAO;
                
                  @In
                  ChannelDAO channelDAO;
                
                  @RequestParameter
                  private Integer channelId;
                
                  private Channel channel;
                
                  private List<Video> channelVideos;
                
                  @Create
                  public void initChannelVideos()
                  {
                    channel = channelDAO.getChannel(channelId);
                    channelVideos = videoDAO.getVideos(channelId, -1, VideoDAO.VIDEO_ORDER_NEW);
                  }
                
                  public List<Video> getChannelVideos()
                  {
                    return channelVideos;
                  }
                
                  public Channel getChannel()
                  {
                    return channel;
                  }
                }
                


                The page is access with this code:


                            <s:link id="linkChannel" view="/channel.xhtml" styleClass="linkChannel">
                              <f:param name="channelId" value="#{channel.channelId}" />
                              #{channel.name}
                            </s:link>



                Now, after the page is loaded, I go to the search box entered some text and press Search button. This gives me a null pointer exception, telling me object couldn't be loaded, since the channel id is null. Obviously, this happens because by pressing search the page is being reloaded (postback) and the parameter is not sent, so the id used to load the page is null.


                How can I do it the right way using Seam?


                I came up with a much longer post than the original one... ;-)

                • 5. Re: Page parameters
                  mail.micke

                  Hi


                  Not sure it this solves the problem, but what about using a <s:button> for the search? <s:button> shouldn't submit the form and perhaps that gets around the problem.... just a random guess which might be worth a try.


                  Also wonder if redirect on he navigation rule will help any? grasping for straws.


                  If all else fails perhaps you could just do a null check in the @Create method of channelAction.


                  - micke