6 Replies Latest reply: Apr 4, 2014 3:23 AM by gimbal2 RSS

    f:param is not working for me! What am I doing wrong?

    user12030320

      I have attached f:parameter tag to component h:commandButton as follows:

       

      <h:commandButton id="commandBtn" value="Reserve" action="#{avlbleTrucksBean.storeData}">

         <f:param name="currentId" value="#{String.valueOf(truck.theIndex)}"></f:param>

      </h:commandButton>

       

      Yes, truck.theIndex is an integer.

      I desire to get the value of param and use it in the managed bean, but I am not able to get the value!

       

      The managed bean is as follows:

      @ManagedBean

      @SessionScoped

      @Named("avlbleTrucksBean")

       

      public class AvlbleTrucksBean implements Serializable {

       

        private int currentId;

       

        // getter and setter methods

        public int getCurrentId() {

            return currentId;

        }

       

        public void setCurrentId(int currentId) {

            this.currentId = currentId;

        }

       

        public String storeData() {

            FacesContext fContext = FacesContext.getCurrentInstance();

            this.currentId = Integer.parseInt(getCurrentIdParam(fContext));

            System.out.println("currentId is: " + currentId); // This doesn't print the value! Why?

        } 

       

        // Get value from "f:param"

        public String getCurrentIdParam(FacesContext fc){

            Map<String,String> param = fc.getExternalContext().getRequestParameterMap();

            return  param.get("currentId");

        }

      }

        • 1. Re: f:param is not working for me! What am I doing wrong?
          gimbal2

          Is this JSF 2.x? You should be able to just pass a parameter to the action method.

           

          http://www.mkyong.com/jsf2/4-ways-to-pass-parameter-from-jsf-page-to-backing-bean/

           

          If it is not JSF 2.x, upgrade today because it makes your life about a hundred times easier. In any case, you know you are using a framework right? You have no need for manual integer to String conversions, that is all handled for you.

          • 2. Re: f:param is not working for me! What am I doing wrong?
            user12030320

            Yes I am using JSF 2.2.

            Indeed I'm now using:

            <ice:commandButton id="commandLink" value="Reserve"

                   partialSubmit="false"

                   action="#{avlbleTrucksBean.storeData(truck.theIndex)}">

            </ice:commandButton>

             

            and in the backing bean I have:

            public String storeData(int Id) {

                    this.currentId = Id;

                    ......

            }

            Still no success! Error must be somewhere else. I'm checking. Thanks all the same!

            • 3. Re: f:param is not working for me! What am I doing wrong?
              gimbal2

              "no success" is very vague. Crash, burn, error, what?

               

              I would advise you to be complete in your problem descriptions by the way, it is prudent to mention for example that you're using icefaces.

              • 4. Re: f:param is not working for me! What am I doing wrong?
                user12030320

                Thanks for the advice. Yes I need to be specific!

                I have switched from using <f:param  ../>

                to

                <ice:commandButton id="commandLink" value="Reserve"

                       partialSubmit="false"

                       action="#{avlbleTrucksBean.storeData(truck.theIndex)}">

                </ice:commandButton>

                where I pass an int value to my backing bean via storeData.

                Incidentally, commandButton is within a dataTable on the jsf page - there is commandButton for each row of my table as shown below:

                 

                <ice:dataTable rows="10" id="matchedTrucks" binding="#{avlbleTrucksBean.theDataTable}"

                    value="#{avlbleTrucksBean.matchedTruckList}" var="truck">

                    ....

                    <ice:commandButton id="commandLink" value="Reserve"

                       partialSubmit="false"

                       action="#{avlbleTrucksBean.storeData(truck.theIndex)}">

                    </ice:commandButton>

                </ice:dataTable>

                 

                I have no success in this:

                -  commandButton action does not execute, even though the backing bean is a session bean.

                - the int value passed to the backing bean is supposed to be used as follows:

                public String storeData(int Id) {

                        // Get currentId of reserved truck

                        this.currentId = Id;

                        System.out.println("currentId: " + currentId); // this does not print indicating commandButton action fails to execute!

                        // Use currentId to get all relevant parameters of the reserved truck

                        someTruck = matchedTruckList.get(currentId);

                        int truckId = someTruck.getTruckId();

                        ....

                 

                 

                return "";

                }

                 

                I do appreciate your kind help.

                • 5. Re: f:param is not working for me! What am I doing wrong?
                  user12030320

                  Well, I consistently used <ice:form instead of mixing <h:form and <ice:form and now all is well.

                  • 6. Re: f:param is not working for me! What am I doing wrong?
                    gimbal2

                    Weird that this would fix it, but I'm glad you figured it out. Thanks for posting back.