one action method handling multiple command buttons

Hi,

I have a bunch of auto-generated command buttons in a page. I need to call the same (i have only ONE action method) action method and decide which button was clicked. Currently i am using a javascript that sets a hidden parameter based on the button clicked. Then i use this value for further processing in my action method. Can i do the similar operation using jsf without using javascript?

Regards,

Mani

[434 byte] By [manikandanca] at [2007-11-26 18:55:12]
# 1

You could a parameter to your commandButton

<h:commandButton value="Press here" actionListener="#{myBean.action}">

<f:attribute name="attrname1" value="attrvalue1" />

</h:commandButton>

or you can do the following

<h:commandButton id="foobar1" image="images/central.jpg"

actionListener="#{test.listen}"/>

public void listen(ActionEvent event) {

Make a list of commandbutton id

UIComponent comp = event.getComponent();

FacesContext context = FacesContext.getCurrentInstance();

String componentId = comp.getClientId(context);// get the id of the button pressed

loop thorugh the list for the matching button name

Hope this helps

ferric4a at 2007-7-9 20:33:12 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Hi ferric4 ,Thanks a lot for your suggestion. The first one worked. I tried the 2nd option before itself. Since the buttons are auto generated, i got an error message similar to that we have to use only static text as id.Thanks,Mani
manikandanca at 2007-7-9 20:33:12 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Hi,

Yesterday i tried with static values in attributes. ie:

<f:attribute name="clicked" value="1" />

I was able to get the values in my listener class.

I want to provide dynamic values to the Action Listener using attributes.

My code is as follows:

<h:dataTable value="#{SampleBean.list}" var="resultObj">

<h:column>

<h:commandButton value="Click here" onclick="javascript:alert('#{resultObj}')"

action="#{SampleBean.action}" actionListener="#{SampleBean.listener}" >

<f:attribute name="clicked" value="#{resultObj}" />

</h:commandButton>

I am getting the alert message with correct values. But in listener (code as follows), im getting null values.

ArrayList<String> list;

public SampleBean() {

System.out.println("Constructor called");

list = new ArrayList<String>();

list.add("1");

list.add("2");

list.add("3");

list.add("4");

}

public void listener(ActionEvent event) {

Iterator iter = event.getComponent().getAttributes().keySet().iterator();

while(iter.hasNext()) {

System.out.println("DEBUG: Value ->" + iter.next());

}

System.out.println("CLICKED : " +

event.getComponent().getAttributes().get("clicked"));

// I am getting null here

}// listener

Any help will be appreciated.

manikandanca at 2007-7-9 20:33:12 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...