Documentation of ui:repeat, attribute value
states that it can iterate over
List, array, java.sql.ResultSet, or an individual java Object
However it seems that int[]
is understood as Object
rather than array. Is that any way how to iterate over array of primitives in JSF 2.2? Or at least why it is not possible?
My code:
@Named
@RequestScoped
public class UiRepeatBean {
public int[] getArray() {
return IntStream.range(0, 4)
.toArray();
}
public List<Integer> getList() {
return IntStream.of(getArray())
.mapToObj(i -> i)
.collect(Collectors.toList());
}
public Integer[] getArrayOfIntegers() {
return IntStream.of(getArray())
.mapToObj(i -> i)
.toArray(Integer[]::new);
}
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<head>
<title>Start Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<h:body>
<p>
int[]
<ui:repeat value="#{uiRepeatBean.array}" var="i">
#{i}
</ui:repeat>
</p>
<p>
List<Integer>
<ui:repeat value="#{uiRepeatBean.list}" var="i">
#{i}
</ui:repeat>
</p>
<p>
Integer[]
<ui:repeat value="#{uiRepeatBean.arrayOfIntegers}" var="i">
#{i}
</ui:repeat>
</p>
</h:body>
</html>
rendered result:
int[] [I@70fcfc34
List<Integer> 0 1 2 3
Integer[] 0 1 2 3
It doesn't work because an array of primitives cannot be cast to Object[]
as required by underlying ArrayDataModel
. An array of primitives is an instance of Object
instead of Object[]
. There is no way to convert an array of primitives to Object[]
other than looping over it and creating a new array. This isn't really the responsibility of a model view presenter framework like JSF. The developer itself is responsible for supplying the right model in first place.
I however do agree that the documentation can be more clarified on this. You can do that by leaving an issue at JSF spec guys.