4 Replies Latest reply on Apr 18, 2013 9:35 AM by Vladimir Aguirre

    Trying to use actionParameter in backing bean unsuccesfully.

    Ben Katz Newbie

      Hi,

       

      I'm trying to use a HtmlActionParameter to report to the backing bean the ID of the button pressed.

      The way I do it is have an HTMLPane over HtmlAjaxSupport over HtmlActionParameter.

       

      HTMLPane

          \------> HtmlAjaxSupport

                          \----------> HtmlActionParameter

       

      I expected this to make HTMLPane clickable with AjaxSupport (That worked) and have ActionParam set a value to be used by the function called.

      The code that implements this is:

       

           private String currentlySelectedActionButton;
           public String addActionButton() {
                // Define Button
                HtmlPanel button = new HtmlPanel();
                String newButtonIndex = Integer.toString(this.actionPanel.getChildren().size());
      
                ExpressionFactory ef = FacesContext
                          .getCurrentInstance().getApplication().getExpressionFactory();
                
                // Add action param to save value
                ValueExpression ve = ef.createValueExpression(
                          FacesContext.getCurrentInstance().getELContext(),
                          "#{actions.CurrentlySelectedActionButton}", String.class);
                System.out.println("New Button Index is "+newButtonIndex);
                HtmlActionParameter actionParam = new HtmlActionParameter();
                actionParam.setValue(newButtonIndex);
                actionParam.setAssignToBinding(ve);
                actionParam.setName("CurrentButton");
                
                
                // Define Action Listener
                MethodExpression me = ef.createMethodExpression(
                          FacesContext.getCurrentInstance().getELContext(), "#{actions.createNewAction}",
                          String.class, new Class[] {});
                HtmlAjaxSupport supportObj = new HtmlAjaxSupport();
                supportObj.setEvent("onclick");
                supportObj.setActionExpression(me);
                supportObj.setReRender("EditPanel");
                
                
                // Add new object to action panel
                supportObj.getChildren().add(actionParam);
                actionParam.setParent(supportObj);
                button.getChildren().add(supportObj);
                supportObj.setParent(button);
                this.actionPanel.getChildren().add(button);
      
                return "Success";
           }
      
           public String createNewAction() {
           //  Do something with this.currentlySelectedActionButton
           }
      

       

      This doesn't work since currentlySelectedActionButton's value doesnt change. (Although createNewAction() is called and tries to access currentlySelectedActionButton, which causes an exception since it is uninitialized.)

       

      I'm wondering what's wrong in this code. Hope that someone has the patience to read my question. :-)

      Thank you!

       

      P.S - I'm doing everything from the backing bean since I need the act of adding buttons to be dynamic.

        • 1. Re: Trying to use actionParameter in backing bean unsuccesfully.
          Nick Belaevski Master

          Hi,

           

          You should also add UIActionParam as action listener:

          supportObj.addActionListener(actionParam);
          
          • 2. Re: Trying to use actionParameter in backing bean unsuccesfully.
            Ben Katz Newbie

            Wow! 2-3 days I tried to make this work! Thank you!

            • 3. Re: Trying to use actionParameter in backing bean unsuccesfully.
              Vladimir Aguirre Newbie

              I'm trying add a action paramater to HtmlMenuItem, but I can't.

               

               

              Application application = FacesContext.getCurrentInstance()

                                                                                    .getApplication();

                                                                MethodExpression methodExpression = application

                                                                                    .getExpressionFactory().createMethodExpression(

                                                                                                        FacesContext.getCurrentInstance()

                                                                                                                            .getELContext(), unaOpc.getUrl(),

                                                                                                        null, new Class[] {});

                                                                itemSec.setActionExpression(methodExpression);

               

                                                                ExpressionFactory ef = FacesContext

                                                      .getCurrentInstance().getApplication().getExpressionFactory();

                                                      // Add action param to save value

                                                      ValueExpression ve = ef.createValueExpression(

                                                                FacesContext.getCurrentInstance().getELContext(),

                                                                "#{registrarPolizaBean.pruebaPG.clavePasoGuia}", String.class);

                                                      HtmlActionParameter actionParam = new HtmlActionParameter();

                                                      actionParam.setValue("603219875");

                                                      actionParam.setAssignToBinding(ve);

                                                      actionParam.setName("CurrentButton");

                                                     

                                                      HtmlAjaxSupport supportObj = new HtmlAjaxSupport();

                                                      supportObj.setEvent("onclick");

              //                                        supportObj.setActionExpression(methodExpression);

                                                      supportObj.addActionListener(actionParam);

                                                     

                                                      // Add new object to action panel

                                                      supportObj.getChildren().add(actionParam);

                                                      actionParam.setParent(supportObj);

                                                      itemSec.getChildren().add(supportObj);

                                                      supportObj.setParent(itemSec);

               


              Please helpme.

              Thanks in advance.

              • 4. Re: Trying to use actionParameter in backing bean unsuccesfully.
                Vladimir Aguirre Newbie

                I found a solution:

                Creating de paramater (original MB):

                 

                                                        UIParameter parameter = new UIParameter();

                                                                            parameter.setName("pasoGuiaDesdeMenu");

                                                                            parameter.setValue(pkHp.getIdStr());

                                                                            itemSec.getChildren().add(parameter);

                 

                Get parameter (another MB):

                 

                                    final String pasoGuiaDesdeMenu = super.getRequest().getParameter(

                  "pasoGuiaDesdeMenu");

                                    logger.info("pasoGuiaDesdeMenu :: " + pasoGuiaDesdeMenu);

                                    if (StringUtils.isNotBlank(pasoGuiaDesdeMenu)) {

                                              PasoGuiaPKHelper pkHp = new PasoGuiaPKHelper(pasoGuiaDesdeMenu);

                                              pasoGuiaIdDTO = pkHp.getIdDto();

                  pasoGuiaDesdeMenuSelected = true;

                                    } else {

                  pasoGuiaDesdeMenuSelected = false;

                                    }

                 

                Thanks!