UI-Component Sets

Home » Wiki » MyFaces Core » MyFaces Core User Guide » JSF and MyFaces Howtos » Working with tables

Get row data from an ActionListener

Handling buttons or links in table rows

If you you have a command link or button in a row of a dataTable, there is an easy way to get to row bean from an javax.faces.event.ActionListener.

<h:dataTable value="#{ResultsBean.hitSet.hits}" var="hit">
  <h:column>
    <h:commandLink>
      <f:actionListener type="net.java.OrderActionListener" />
      <h:outputText value="Order" />
    </h:commandLink>
    ...
  </h:column>
</h:dataTable>

By this simple Java code in your subclass of javax.faces.event.ActionListener you get the row bean.

public class OrderActionListener implements ActionListener {

  public void processAction(ActionEvent anEvent) throws
AbortProcessingException {

    YourBeanClass tmpBean = null;

    UIComponent tmpComponent = anEvent.getComponent();

    while (null != tmpComponent && !(tmpComponent instanceof UIData)) {
      tmpComponent = tmpComponent.getParent();
    }

    if (tmpComponent != null && (tmpComponent instanceof UIData)) {
      Object tmpRowData = ((UIData) tmpComponent).getRowData();
      if (tmpRowData instanceof YourBeanClass) {
    tmpBean = (YourBeanClass) tmpRowData;

    //TODO Implementation of your method

      }
    }

    //TODO Exception Handling if UIData not found or tmpRowBean of wrong
type

  }
}