wsxiapiaoxue
wsxiapiaoxue

Reputation: 47

How to pass ID of a dynamically generated button to action method?

I created a Facelets page. In the page dynamically generated some buttons, each button has a ID, When submitted the event, I want to pass a button's ID to the action.(button is dynamically)

But i don't know how to pass dynamic parameters in JSF.

Upvotes: 1

Views: 1264

Answers (1)

BalusC
BalusC

Reputation: 1108742

You can get the button component in the action method by UIComponent#getCurrentComponent():

public void submit() {
    UIComponent button = UIComponent.getCurrentComponent(FacesContext.getCurrentInstance());
    String id = button.getId(); // or button.getClientId();
    // ...
}

Alternatively, if you're targeting a Servlet 3.0 / EL 2.2 container (Tomcat 7, Glassfish 3, etc), then you can just invoke methods with arguments in EL:

<h:commandButton action="#{bean.submit(component.id)}" /> <!-- or component.clientId -->

with

public void submit(String id) {
    // ...
}

Upvotes: 2

Related Questions