0

Mojarra 2.2

I have two beans`.

public class MyBean1{

    private String myProperty1;
    //GET, SET, CTOR

    public void doAction(){
         //assign something to myProperty1
    }
}

public class MyBean2{

    private String myProperty2;
    //GET, SET, CTOR
}

Now, I need to assign the value of the property MyBean1::myProperty1 to MyBean2::myProperty2 after the doAction() method invocation by clicking a button:

<h:commandButton action="#{myBean1.doAction}">
     <f:setPropertyActionListener target="#{myBean2.myProperty2}"
                                   value="#{myBean1.myProperty1}" />
</h:commandButton>

But it doesn't work. And I figured out that it doesn't work due to the following reason:

Clicking a button causes ActionEvent to be broadcasted. It performs by this method (javax.faces.component.UICommand):

public void broadcast(FacesEvent event) throws AbortProcessingException {

    super.broadcast(event); //1       <-------------------- HERE

    if (event instanceof ActionEvent) {
        FacesContext context = getFacesContext();


        MethodBinding mb = getActionListener();
        if (mb != null) {
            mb.invoke(context, new Object[] { event });
        }


        ActionListener listener =
        context.getApplication().getActionListener();
        if (listener != null) {
            listener.processAction((ActionEvent) event);
        }
    }
}

I've noticed that the ActionEvent broadcasting by clickng the button is being handled by two listeners and one that comes from super.broadcast(event) invokation at //1 is com.sun.faces.facelets.tag.jsf.core.SetPropertyActionListenerHandler.SetPropertyListener.

The handling is performed before invokation of the action method.

Of course, I can embed the action method invokation into the MyBean1:getMyProperty1() getter, so the myProperty1 field will be properly intialized, but it seems quite wierd to me. What is the right way to achieve that?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
St.Antario
  • 26,175
  • 41
  • 130
  • 318
  • The value of `myBean2.myProperty2` is likely to be unavailable at the moment the button is clicked. There could be a scoped-related problem or the value of that property is not set or initialized explicitly before the button is clicked. – Tiny Oct 29 '15 at 14:46
  • @Tiny Sorry, it was a typo. Checked – St.Antario Oct 29 '15 at 14:47
  • Related: http://stackoverflow.com/questions/3909267/differences-between-action-and-actionlistener – BalusC Oct 29 '15 at 14:56

0 Answers0