2

I have tried several ways to implement and adding programatically a commandButon with action or actionListener. I tried to implement an ajax behavior, set an action and set an actionListener without success. In my pom I added el-api dependence because my project didn't got MethodExpression,MethodExpressionActionListener and ExpressionFactory classes and they were necessary in some example codes. I don't get any error messages on glassfish log neither js log.

Edit If I change bean scope to Session the action method is invoked but never with request or view on this balus example

Methods' BeanUtils class

    public static MethodExpression createMethodExpression(String valueExpression,Class<?> expectedReturnType,Class<?>[] expectedParamTypes) {

    MethodExpression methodExpression = null;

    try {
        FacesContext fc = FacesContext.getCurrentInstance();
        ExpressionFactory factory = fc.getApplication().getExpressionFactory();

        methodExpression = factory.createMethodExpression(fc.getELContext(), valueExpression, expectedReturnType, expectedParamTypes);
    } catch (Exception e) {
        throw new FacesException("Method expression '" + valueExpression + "' could not be created.");
    }

    return methodExpression;
}

public static MethodExpressionActionListener createMethodActionListener(String valueExpression,Class<?> expectedReturnType,Class<?>[] expectedParamTypes) {

    MethodExpressionActionListener actionListener = null;

    try {
        actionListener = new MethodExpressionActionListener(createMethodExpression(
        valueExpression, expectedReturnType, expectedParamTypes));
    } catch (Exception e) {
        throw new FacesException("Method expression for ActionListener '" + valueExpression
        + "' could not be created.");
    }

    return actionListener;
}

Button code (same for every try)

            CommandButton cancelBtn=(CommandButton)app.createComponent(CommandButton.COMPONENT_TYPE);
            cancelBtn.setId("deleteText_btn_" + panel.getId());
            cancelBtn.setAlt("Change");
            cancelBtn.setTitle("Change");
            cancelBtn.setValue("Change");

First try - Ajax behavior

                MethodExpression me = BeanUtils.createMethodExpression("#{eventProvider.deleteText()}",null,new Class<?>[]{BehaviorEvent.class});
            AjaxBehavior ajaxBehavior = (AjaxBehavior) app.createBehavior(AjaxBehavior.BEHAVIOR_ID);
            ajaxBehavior.addAjaxBehaviorListener(new AjaxBehaviorListenerImpl(me, me));

            cancelBtn.addClientBehavior("submit",ajaxBehavior);

Second try - setting an actionListener

                FacesContext context = FacesContextWrapper.getCurrentInstance();
            MethodExpressionActionListener methodExpression = new MethodExpressionActionListener(context.getApplication().getExpressionFactory()
                    .createMethodExpression(context.getELContext(),"#{eventProvider.deleteText}", null, new Class[] {ActionEvent.class}));
            cancelBtn.addActionListener(methodExpression);

Third try - Setting an action

            FacesContext context = FacesContext.getCurrentInstance();
            MethodExpression methodExpression = context.getApplication().getExpressionFactory().createMethodExpression(
                context.getELContext(), "#{eventProvider.deleteText}", null, new Class[] { ActionEvent.class });
            cancelBtn.setActionExpression(methodExpression);

Method which must be called from action/actionListener

   @Component("eventProvider")
   @Scope("view")
   public class EventProvider {
    ............
    ............
    public void deleteText(ActionEvent event){
    logger.entry("EventProvider.deleteText()");

     System.out.print("debbuging");
    }
}
Community
  • 1
  • 1
  • What is it that you basically accomplish by adding Command buttons programmatically from managedbean? – Kishor Prakash Dec 01 '14 at 04:23
  • Because there is a wizard which got 3 buttons to create panelgroups, 1 for adding text, 1 for gmap and last one for picture, they are draggable, resizable and dynamic content. As these panelgroups are added dynamically I need to insert a button to delete it or save it. Idea is to create a interactive gui or similar where each user can define his own view. – Rafael Ruiz Tabares Dec 01 '14 at 19:05
  • Why cant you do it with the rendered attribute and still do it in the xhtml? – Jaqen H'ghar Dec 04 '14 at 07:42
  • @JaqenH'ghar how would you do? Because button are inside a panel which will be created dynamically by a method, so I need to create the buttons in the same method and adding to the panel, isn't? – Rafael Ruiz Tabares Dec 04 '14 at 10:02
  • Did you solve this problem ? I am using primefaces 3.5 and by far I don't come up with solution – Michal Heneš Mar 12 '15 at 13:08
  • Unfortunately no. Instead of to use a dynamic button, dynamically I created a div and onclick attribute I setted a method which delete layer using an identifier. Please, post id if you find it! :) it will be very helpful – Rafael Ruiz Tabares Mar 12 '15 at 14:12
  • No solution found yet? I have the same problem, using PF 5.3 – Neo Dec 28 '15 at 10:54

0 Answers0