Thursday, February 08, 2007

JSF: Binding dataTable from managed bean

This is a very easy method to retrieve row data of the data table in jsp page, from managed bean.
<h:dataTable var="item" value="#{MyBean.items}"
binding="#{MyBean.dataTable}" >
<h:outputText styleClass="output" value="#{item.productName}"/>

<h:commandButton value="remove" action="#{MyBean.remove}" />
public class MyBean {
   private ArrayList items = new ArrayList();
   private HtmlDataTable dataTable;

   public ArrayList getItems() {
      return items;

   public void setItems(List items) {
      this.items = new ArrayList(items);

   public void remove(){
      ItemBean item = (ItemBean) getDataTable().getRowData();

   public HtmlDataTable getDataTable() {
      return dataTable;

   public void setDataTable(HtmlDataTable dataTable){
      this.dataTable = dataTable;

here, when user clicks the remove button, the related item is removed
from the data table.


Unknown said...

I got stuck with such scenario.
Can u please provide explanation for this example.

Thank You.

rafael said...

Excellent post! Thank you very much!

Unknown said...

sure, nice post and nice lookin!

Unknown said...

helped me a lot... thanks...

PULSAR said...

Can't you people for once make a step by step example for us beginners?!!! That would also be comprehesive?!!! arggg

Unknown said...

i would like to know about the ItemBean class used in the remove methode in this line
ItemBean item = (ItemBean) getDataTable().getRowData();

i didnt find any doc about that class i would like to know more about it !

­ said...

ItemBean is the referenced type in dataTable, since getRowData() return Object, you need cast that guy :-)

Kale Co Jakim said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.

siva said...

All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information,
I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.
Java training in Btm layout
Java training in Jaya nagar
Java training in Electronic city
Java training in Chennai
Java training in USA

Related Posts with Thumbnails