Getting started with custom loop component

Explanation


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
	<table> 
		<caption>Loop getting started</caption> 
		<tr> 
			<td>Name</td> 
			<td>Phone</td> 
			<td>Index</td> 
		</tr> 
		<iceext:loop value="#{loopGettingStartedBean.rows}" var="row" rowIndexVar="rowIndexVar" > 
			<tr> 
				<td><ice:outputText value="#{row.name}" /></td> 
				<td><ice:outputText value="#{row.phone}" /></td> 
				<td><ice:outputText value="#{rowIndexVar}" /></td> 
			</tr> 
		</iceext:loop> 
	</table> 

This is main entry point. Its referenced by 'loopGettingStartedBean' from faces-config.xml
com.gpost.jsfexamples.loopGettingStarted.LoopGettingStartedBean
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'.
com.gpost.jsfexamples.loopGettingStarted.ManWithPhone
package com.gpost.jsfexamples.loopGettingStarted;

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

	String name;
	
	String phone;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}
}


part of faces-config.xml
<managed-bean>
 <managed-bean-name>loopGettingStartedBean</managed-bean-name>
 <managed-bean-class>com.gpost.jsfexamples.loopGettingStarted.LoopGettingStartedBean</managed-bean-class>
 <managed-bean-scope>session</managed-bean-scope>

</managed-bean>

conclusion
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