1 Reply Latest reply on Feb 13, 2010 1:13 PM by Piotr Sobczyk

    Binding action method to programically created component doesn't work

    Piotr Sobczyk Newbie

      I trimmed down my custom Facelets component to absolutely minimum:


      public class MyComponent extends UIOutput{
      
      ...
      
              @Override
           public void encodeBegin(FacesContext ctx) throws IOException {
                HtmlForm form = new HtmlForm();
                HtmlCommandLink link = createTestCommandLink();
                form.getChildren().add(link);
                link.setParent(form);
                form.encodeAll(ctx);
           }
      
              private HtmlCommandLink createTestCommandLink(){
                HtmlCommandLink testCommandLink = new HtmlCommandLink();
                testCommandLink.setValue("test");
                
                MethodExpression methodExpr = FacesContext.getCurrentInstance()
                   .getApplication().getExpressionFactory()
                   .createMethodExpression(FacesContext.getCurrentInstance().getELContext(),
                   "#{testBean.testAction}",null,new Class[0]);
                
                testCommandLink.setActionExpression(methodExpr);
                return testCommandLink;
           }
      ...
      }



      @Name("testBean")
      @Scope(ScopeType.EVENT)
      public class TestBean implements Serializable{
      ...
         public void testAction(){
           log.info("testAction invoked");
         }
      ...
      }



      However my action still isn't invoked when I click the link. What am I doing wrong here? I searched a lot and saw everywhere template that I used here to do it. So why id doesn't work?



        • 1. Re: Binding action method to programically created component doesn't work
          Piotr Sobczyk Newbie

          Allright, I solved it!


          My custom component was lacking overriden decode method that generates event. I make my component extending UIComand (and in that way getting access to protected queueEvent method) and added such code:


          @Override
          public void decode(FacesContext ctx){
               ExternalContext external = ctx.getExternalContext();
                  Map requestParams = external.getRequestParameterMap();
          
                  String clientId = link.getClientId(ctx);
                  if(requestParams.containsKey(clientId)){
                       queueEvent(new ActionEvent(link));
                  }
          }



          Maybe it will help someone with similar problem.