|
We have just seen with previous example how the conversion model works for JSF and helps programmer to change the display by just changing the attribute value. The conversion model also helps programmer not to worry about the type casting of the data types during the development. For example, if any text field at the UI accepts the integer and in backing bean if it is mapped to Integer class then JSF conversion model automatically type cast the variable from presentation layer to model layer implicitly.
The representation of data in table format is no longer a worry now. JSF provide <h:dataTable> tag to represent the data in the table. The table can be bound to getter method of the backing bean to get the data and display in the tabular form.<h:column> tag is used to set the column values and header for the data.
Example:- Lets make an paging application which will display the text field for size of a single page and the current page number. The user will fill the information and datatable will only display the data for the particular page.
Step 1:- Create the backing bean PageBean for the example which has startIndex to show where to start display, the size of the page and the current page number.
package com.visualbuilder;
import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.Locale;
public class PageBean {
ArrayList<RegistrationBean> list; Integer startIndex = new Integer(0); Integer size = new Integer(1); Integer pageNo = new Integer(1);
public Integer getPageNo() { return pageNo; }
public void setPageNo(Integer pageNo) { if (pageNo.intValue() > 10) { this.pageNo = 10; } else if (pageNo.intValue() <= 1) { this.pageNo = 1; } else { this.pageNo = pageNo; } }
public ArrayList getItemList() { list = new ArrayList<RegistrationBean>(); for (int i = 0; i <= 10; i++) { RegistrationBean bean = new RegistrationBean(); bean.setName("VisualBuilder" + i); bean.setPassword("test" + i); bean.setDescription("Description" + i); list.add(bean); } return list; }
public Integer getSize() { return size; }
public void setSize(Integer size) { if (size.intValue() > 10) { this.size = 10; } else if (size.intValue() <= 1) { this.size = 1; } else { this.size = size; } }
public Integer getStartIndex() { int index = (pageNo.intValue() - 1) * size.intValue() ; if (index<= 0) { startIndex = new Integer(0); } else if (index> 10) { startIndex = new Integer(11 - size.intValue()); } else { startIndex = new Integer(index);
}
return startIndex; }
public void setStartIndex(Integer startindex) { this.startIndex = startindex; } } |