Loop over a list of model elements with an iterator faclet tag in JSF
What about
- If your container component has specific requirements on the type of its child component you can't go with the known tags because they put there own components as child-components on the container.
- Both of them cannot be used if you need a dynamic evaluation of model elements
You can use the tag as follows to iterator over a list of employees to print out there names:
<my:iterator value="#{bean.employees}" var="employee" > <<tr:outputText value="#{employee.name}" /> </my:iterator>
The Tag assumes a value of type List.
The Java Code of the Facelets Tag Handler:
public class IteratorTag extends TagHandler
{
private final TagAttribute _value;
private final TagAttribute _var;
public IteratorTag(TagConfig p_arg0)
{
super(p_arg0);
_value = getRequiredAttribute("value");
_var = getRequiredAttribute("var");
}
public void apply(FaceletContext p_facelets,
UIComponent p_parentComponent) throws IOException, FacesException, FaceletException, ELException
{
// get the value binding behind attr "value"
List list = (List) _value.getObject(p_facelets, List.class);
int idIndex = 0;
for (Object object : list)
{
// get the name of the variable
String varName = _var.getValue(p_facelets);
// set new variable into faclets attribute map
p_facelets.setAttribute(varName, object);
nextHandler.apply(p_facelets, p_parentComponent);
// update ID of last created component from nextHandler
String clientId = p_parentComponent.getClientId(p_facelets.getFacesContext()) + "_dynamicChild"+idIndex;
List
children = p_parentComponent.getChildren(); children.get(children.size()-1).setId(clientId);
idIndex++;
}
}
}
0 Comments:
Post a Comment
<< Home