Getting started with custom loop component


New to Java, JSF, Icefaces? Here is some links to start with:
Why and when use ICEFACES?
First steps with jsf and java
icefaces without icefaces

We know ui:repeat tag. Generally it loop on inner tags. Real application need more features.
Just few examples: paging, need for "index", different styles for odd item.
This simple yet important features are difficult to implement with "ui:repeat" tag
So instead of ui:repeat, use custom "loop" component.
Pros: every programmer, even not-jsf, can understand it. CSS is transparent: what you see is what you get.
Customization is simple too.
Use you favorite HTML editor, or JSF editor together with CSS tools to customize every aspect of this example, its easy!
fragment from LoopGettingStarted.jspx
		<caption>Loop getting started</caption> 
		<iceext:loop value="#{loopGettingStartedBean.rows}" var="row" rowIndexVar="rowIndexVar" > 
				<td><ice:outputText value="#{}" /></td> 
				<td><ice:outputText value="#{}" /></td> 
				<td><ice:outputText value="#{rowIndexVar}" /></td> 

This is main entry point. Its referenced by 'loopGettingStartedBean' from faces-config.xml
package com.gpost.jsfexamples.loopGettingStarted;

import java.util.ArrayList;
import java.util.List;

* Java class for example 'Loop getting started'
* referenced as 'loopGettingStartedBean' from faces-config.xml, 
* used in 'LoopGettingStarted.jspx'
public class LoopGettingStartedBean{

	List<ManWithPhone> rows = null;
	public List<ManWithPhone> getRows() {
		if (rows == null) {
			rows = new ArrayList<ManWithPhone>();
			// create same sample data...
			rows.add(new ManWithPhone("Gary", "1234-6767"));
			rows.add(new ManWithPhone("Bob", "5678-4553"));
			rows.add(new ManWithPhone("John", "5698-4333"));
		return rows;

} // end of class

This class defines data in 'row'.
package com.gpost.jsfexamples.loopGettingStarted;

public class ManWithPhone {
	public ManWithPhone(String name, String phone) {
		super(); = name; = phone;

	String name;
	String phone;

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public String getPhone() {
		return phone;

	public void setPhone(String phone) { = phone;

part of faces-config.xml


As we saw in this example, you can use loop with simple html/css, then add some binding to your java beans and you get very flexible, easily customizable layout with more features like paging, indexing, and mod variables.

No comments:

Post a Comment