Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can't make ui:repeat work

 
Khalid Alghamdi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I have a web app with ui:repeat and h:dataTable tags. the repeat tag does not work while dataTable works. what did I do wrong in here. see code below

My index.xhtml has the following contens:

= Start ============================
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"<br/> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"<br/> xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">


<h1>Repeat Tag</h1>
<ul>
<ui:repeat var="color1" value="#{colorsBean.colors}">
<li>#{color1}</li>
</ui:repeat>
</ul>

<hr/>

<h1>DataTable Tag</h1>
<ul>
<h:dataTable var="color2" value="#{colorsBean.colors}">
<h:column>
<f:facet name="header">
<h:outputText value="Color" />
</f:facet>
<h:outputText value="#{color2}"/>
</h:column>
</h:dataTable>
</ul>
</html>

= End ============================



ColorsBean is as follows:

== Start ===========================
public class ColorsBean {
String []colors = new String[] {"White","Black","Freen","Blue","Red","Yellow","Cyan"};

public String[] getColors() {
return colors;
}

public void setColors(String[] colors) {
this.colors = colors;
}
}
= End ============================

the libraries I have under WEB-INF/lib are:
- jsf-api.jar
- jsf-facelets-1.1.11.jar
- jsf-impl.jar

==============================

here is the page output:

Repeat Tag
--------------------------------------------------------------------------------
DataTable Tag
Color
White
Black
Freen
Blue
Red
Yellow
Cyan
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18094
48
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Khalid!

We're having a special on that question this week.

I'll give the same response as I have to everyone else: Why not simply use a dataTable?
 
Khalid Alghamdi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:Welcome to the JavaRanch, Khalid!

We're having a special on that question this week.

I'll give the same response as I have to everyone else: Why not simply use a dataTable?


I would like to use ui:repeat within a <h:selectOneMenu> tag
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18094
48
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khalid Alghamdi wrote:
Tim Holloway wrote:Welcome to the JavaRanch, Khalid!

We're having a special on that question this week.

I'll give the same response as I have to everyone else: Why not simply use a dataTable?


I would like to use ui:repeat within a <h:selectOneMenu> tag


Ah. Today is my day to be completely and stupidly blind. Speaking of going blind, there's a "Code" button on our message editor. You can use it to wrap java code, xml and other things that the normal text editor won't display well.

I don't think you're going to be able to use ui:repeat there.

A JSF page is not a program like a normal JSP is. It's a declaration. So trying to do program-like things on a JSF view often fails, as can be seen by all the people who are asking questions like yours.

Beyond that, the JSF tags have very specifically-defined interrelationships. You can't validly insert a textInput control within a textOutput control for example.

For a DataTable, you need a DataModel to serve as the UI sub-model in the JSF Model/View/Controller setup. In most cases your table itself will be an array or List of elements of some sort of class, with the properties of the class serving as column names. In your case, for example:



You could then create a List of ColorItems, wrap them with a ListDataModel object and return that object to the dataTable in the View as the dataTable's "value" attribute.

All this is awkward, if not impossible to do in a JSF view. But then, that's what Backing Beans are for.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic