1 2 Previous Next 17 Replies Latest reply on Sep 28, 2007 6:10 PM by cupdike

    How can I pass parameters to action methods

    javarebel

      I need to pass a parameter to an action method in the backing bean, how can I do that? Now I am getting the parameter as Null in the action method.

      My XHTML Page
      -----------------
      <h:form id="myForm">
      <c:forEach items="#{listingSearch.pagesLinks}" var="page">
      <s:link action="#{listingSearch.next(page.globalListingId)}"
      value="#{page.globalListingId}" />
      </c:forEach>
      </h:form>

      where:
      listingSearch is the name of the Stateful Session Bean
      pagesLinks is a private list, which have a getter method defined.
      globalListingId is a property of the pojo which I am putting in the list(pagesLinks).


      My Action Method
      ----------------------
      public void next(Object object) {
      System.out.println("Parameter -------->" + object);
      return ;
      }


      In the web page the value part of the <s:link> is correctly resolved and it is displaying the globalListingId values from the list. But in the backing bean action method I am getting the object parameter as null.

        • 1. Re: How can I pass parameters to action methods
          christian.bauer

          You need to understand the JSF request lifecycle. In the request in which your page is rendered, page.globalListingsId can be successfully resolved in some context. Apparently it can't be resolved in any context (session, conversation, event, etc.) when you do the next request and execute the action. The callThisAction(parameter) syntax in the EL is really just a shortcut for a lookup/injection of that "parameter" contextual variable that happens when the action is executed.

          • 2. Re: How can I pass parameters to action methods
            javarebel

            My requirement is to get an identifier in which link I have clicked in the backing bean. I am able to find a solution for this.

            In XHTML File
            -------------------------
            <c:forEach items="#{listingSearch.pagesLinks}" var="page">
            <s:link action="#{listingSearch.next()}"
            value="#{page.globalListingId}">
            <f:param name="linkId" value="#{page.globalListingId}" />
            </s:link>
            </c:forEach>

            In Backing bean
            -----------------------

            @RequestParameter
            private String linkId;

            public void next() {
            System.out.println("You Clicked on link Id : " + this.linkId);
            return;
            }

            or

            FacesContext facesContext = FacesContext.getCurrentInstance();
            HttpServletRequest request = (HttpServletRequest)
            facesContext.getExternalContext().getRequest();
            String linkIdFromCtx = ((String[])request.getParameterMap()
            .get("linkId"))[0];
            System.out.println("Ctx: You Clicked on link Id : " + linkIdFromCtx);

            • 3. Re: How can I pass parameters to action methods
              christian.bauer

              You need to look at the Seam example applications, or really, any JSF example application. I often had the same reflex when I started learning JSF and tried the same approach as you. You can not pass "objects" between requests by putting them into a link.

              Check the booking demo app and how it uses @DataModel and @DataModelSelection, this is what you probably want.

              • 4. Re: How can I pass parameters to action methods
                javarebel

                I can't use DataModel and DataModelSelection because I can't use DataTable. I need to render the links horizontally. Is there anyway I can render DataTable horizontally?

                • 5. Re: How can I pass parameters to action methods
                  christian.bauer

                  So you need to find another way to pass an "object" from request to request. One way is to pass its identifier and to load it from the database in the second request. Another is to stick it into the session an retrieve it from there by identifier.

                  And the last and best way is to put it into the conversation context in the first request, name the context variable in the action method as a parameter, and let Seam look it up from the conversation context in the second request. This is really what Seam is about.

                  • 6. Re: How can I pass parameters to action methods
                    christian.bauer

                    Btw, don't use JSTL with JSF, it's a bad combination. You are already using Facelets, so add xmlns:ui="http://java.sun.com/jsf/facelets"
                    and work with <ui:repeat> instead.

                    • 7. Re: How can I pass parameters to action methods
                      spambob

                      Sorry to jump in here but I actually had the same problem!

                      "christian.bauer@jboss.com" wrote:
                      ...And the last and best way is to put it into the conversation context in the first request, name the context variable in the action method as a parameter, and let Seam look it up from the conversation context in the second request. This is really what Seam is about.

                      As far as I understood it he has a list as a context variable and wants to pass the id of an object contained in the list as action method parameter.

                      So the list is within conversation context but JSF doesn't resolve the id to some string that is therefore null but it would work if he would write "listingSearch.next(3) - in case page.globalListingId would be 3.

                      So is there any way to force JSF to resolve "page.globalListingId" to a string (assuming it is an Integer, String, ... - anything that can be cast to string without problems)?

                      I would greatly appreciate any hints / 2 line examples because this has been some pain for me too.

                      Thanks a lot in advance!

                      • 8. Re: How can I pass parameters to action methods
                        christian.bauer

                        No, JSF is dumb. You could create a Facelets function and call it like this: #{myFunctions:myToString(o)}

                        • 9. Re: How can I pass parameters to action methods
                          spambob

                          Ahh, I see.

                          Thank you very much!

                          • 10. Re: How can I pass parameters to action methods
                            monkeyden

                            Can you define variable argument EL functions in tld files, as in:

                            <function-signature>
                             java.lang.String concat(java.lang.String...)
                            </function-signature>


                            public static String concat(String... strings) {
                             StringBuilder sb = new StringBuilder();
                             for (int j = 0; j < strings.length; j++)
                             sb.append(strings[j]);
                             return sb.toString();
                            }


                            • 11. Re: How can I pass parameters to action methods
                              gavin.king

                              No, its not supported by EL.

                              • 12. Re: How can I pass parameters to action methods
                                cupdike

                                 

                                "christian.bauer@jboss.com" wrote:
                                So you need to find another way to pass an "object" from request to request. One way is to pass its identifier and to load it from the database in the second request. Another is to stick it into the session an retrieve it from there by identifier.

                                But the booking app does pass the object as a parameter [from main.xhtml]:

                                <h:dataTable id="hotels" value="#{hotels}" var="hot" rendered="#{hotels.rowCount>0}">
                                ...
                                 <s:link id="viewHotel" value="View Hotel" action="#{hotelBooking.selectHotel(hot)}"/>


                                The only reason I can guess why this works is that the hotels DataModel property is outjected from a session-scoped SFSB. However, I read in one of Dan Allen's articles on IBM that
                                Outjected variables are placed directly into the variable context, independent of their backing bean.

                                So this suggests that it shouldn't matter.

                                Continuing...
                                "christian.bauer@jboss.com" wrote:
                                And the last and best way is to put it into the conversation context in the first request, name the context variable in the action method as a parameter, and let Seam look it up from the conversation context in the second request. This is really what Seam is about.


                                Doesn't it depend on what the bounds of the conversation are? According to the Yuan/Heute book,
                                The default conversation scope spans only two pages

                                However, I find this statement a bit misleading. If you read further, it suggests that is really a single request/response cycle. And wouldn't this then explain why trying to pass the object using default conversation scope doesn't work? But then how does it work in the booking app, unless the scope of the component outjecting the property has some influence on the lifecycle of the outjected property.

                                Clearly I'm missing something I need to know about how all this works. Please someone explain.

                                TIA, Clark

                                • 13. Re: How can I pass parameters to action methods
                                  cupdike

                                  Apologies to Yuan/Heute, there's really nothing misleading about their statement. They spelled it out pretty clearly when I reread section 7.1.

                                  • 14. Re: How can I pass parameters to action methods
                                    christian.bauer

                                     

                                    But the booking app does pass the object as a parameter [from main.xhtml]:


                                    No, it's magic.

                                    http://jira.jboss.com/jira/browse/JBSEAM-1734

                                    1 2 Previous Next