I am using JSF 2.2 and I want to display a title
attribute on each option
element generated by h:selectOneMenu
with passthrough by using an attribute of the f:selectItems
variable.
It seems that I cannot access the f:selectItems
variable to customize my passthrough attribute
Here is what I have done so far
My entity to display
public class ItemBean {
private int id;
private String strName;
private String strDescription;
public ItemBean(int id, String strName, String strDescription) {
this.id = id;
this.strName = strName;
this.strDescription = strDescription;
}
// Getters and Setters
}
My backbean method to retrieve the list of entities
public List<ItemBean> getItems() {
return new ArrayList<ItemBean>(){
{
add(new ItemBean(1, "Java", "Java programming language"));
add(new ItemBean(2, "PHP", "Yet another language"));
add(new ItemBean(3, "Python", "Not a snake at all"));
}
};
}
My h:selectOneMenu
in the view
<h:selectOneMenu>
<f:selectItems value="#{bean.items}" var="item"
itemValue="#{item.id}"
itemLabel="#{item.strName}"
p:title="Description : #{item.strDescription}"/>
</h:selectOneMenu>
The problem is that I cannot access the item
variable for p:title
, the output is just empty there.
Here is the code generated
<select>
<option title="Description : " value="1">Java</option>
<option title="Description : " value="2">PHP</option>
<option title="Description : " value="3">Python</option>
</select>
Is it possible to do it like that or is there another way ?
I finally found a solution to my problem using jstl
c:forEach
loop and f:selectItem
from this post Using f:selectItems var in passtrough attribute
Here is the code:
<h:selectOneMenu>
<c:forEach items="#{bean.items}" var="item">
<f:selectItem itemValue="#{item.id}"
itemLabel="#{item.strName}"
p:title="Description : #{item.strDescription}"/>
</c:forEach>
</h:selectOneMenu>