Multiple actionlisteners in JSF

前端 未结 2 1282
别跟我提以往
别跟我提以往 2021-01-12 05:13

I want to use multiple action listener to set state of two backing beans before further processing

1st way:



        
相关标签:
2条回答
  • 2021-01-12 05:28

    I see you facilitate the traditional approach of guess-how-it-works-using-bare-intuition-and-random-associations-then-act-surprised :-)

    f:actionListener only lets you add a whole object as an observer, not an arbitrary method. You can either use type attribute to specify the class name (it will be instantiated by JSF) or binding attribute to give an instance of the object that you created by yourself (not a method!). The object must implement javax.faces.event.ActionListener.

    Your second try (testDeviceGroupController.prepareCreate(event)) is wrong on many levels, but the crux is that the methods are called not to handle your action, but to create the Actionlistener instance.

    You have a couple of options:

    • the sanest one: just make a method that calls each of the target methods. Since they are on different beans, you can inject one into the other.
    • if that doesn't work for you, you can create a method that creates a listener object.

    Like this:

    public ActionListener createActionListener() {
        return new ActionListener() {
            @Override
            public void processAction(ActionEvent event) throws AbortProcessingException {
                System.out.println("here I have both the event object, and access to the enclosing bean");
            }
        };
    }
    

    and use it like this:

    <h:commandButton>
        <f:actionListener binding="#{whateverBean.createActionListener()}"/>            
    </h:commandButton>
    
    0 讨论(0)
  • 2021-01-12 05:36

    The binding attribute value needs to point to an object implementing ActionListener interface, not a method.

    From the documentation of f:actionListener's bindig attribute :

    Value binding expression that evaluates to an object that implements javax.faces.event.ActionListener.

    A similar problem was discussed here.

    0 讨论(0)
提交回复
热议问题