0
votes

I'm working with h:selectOneMenu and want to set label and value for it then the UI option list will display label and in my Bean can get the selected value. I have this code working fine:

<h:selectOneMenu value="#{myBean.selected}">
    <f:selectItems value="#{myBean.myList}"/>
</h:selectOneMenu>

But problem appear when I try to add var to set label and value by adding the code below:

<h:selectOneMenu value="#{myBean.selected}">
    <f:selectItems value="#{myBean.myList}" var="field" 
    itemLabel="#{field.label}" itemValue="#{field.value}"/>
</h:selectOneMenu>

The error show Attribute var invalid for tag selectItems according to TLD.

My question are:

Can I resolve this?

If "YES" then "HOW"?

If "NO" then any other way for me to done this?

Thanks

1

1 Answers

3
votes

Attribute var invalid for tag selectItems according to TLD

The <f:selectItems var> attribute was introduced in JSF 2.0 for Facelets. This error suggests either you're using legacy JSF 1.x instead of the newer JSF 2.x where <f:selectItems> lacks the var attribute, or you're using legacy JSP instead of its successor Facelets as JSP is deprecated since JSF 2.0 and didn't get any of new JSF 2.0 tags/attributes. The exact error message is due to the JSP-specific term "TLD" however recognizable to come from JSP parser, which in turn confirms more that you're actually using JSP instead of Facelets.

In order to use <f:selectItems var>, you need to make sure of the following:

  • You're using JSF 2.x instead of JSF 1.x.
  • You're using Facelets instead of JSP.

If you can't upgrade/migrate, then you need to fall back to legacy SelectItem approach in backing bean. You can convert from your List<Field> to a List<SelectItem> as below:

private List<Field> fields;
private List<SelectItem> selectItems; // +getter

@PostConstruct
public void init() {
    fields = yourFieldService.list();
    selectItems = new ArrayList<>();

    for (Field field : fields) {
        selectItems.add(new SelectItem(field.getValue(), field.getLabel()));
    }
}

<f:selectItems value="#{bean.selectItems}" />

See also: