Pratik Makune
Pratik Makune

Reputation: 31

java.lang.String cannot be cast to javax.faces.model.SelectItem with s:selectItems

<h:selectOneMenu id="reportType" style="width:175px" value="#{pageDemoSearchBean.status}">
    <f:selectItems value="#{pageDemoSearchBean.vehicleStatus}" />
</h:selectOneMenu>

I am using JSF 1.2

This lines of code throws a classCastException

java.lang.String cannot be cast to javax.faces.model.SelectItem

Provided that replacing "value" by "itemValue" in f:selectItems tag is giving another exception which tells that

itemValue is incorrect for selectItems according to TLD

Upvotes: 1

Views: 5222

Answers (1)

BalusC
BalusC

Reputation: 1108722

In JSF 1.x, it is not possible to provide a List<T> as <f:selectItems value>. It only supports List<javax.faces.model.SelectItem>.

Here's a kickoff example of proper usage, copypasted from our  tag wiki page (which is written with JSF 2.x in mind, so take into account that some examples won't work for the jurassic JSF 1.x):

<h:form>
    <h:selectOneMenu value="#{bean.selectedItem}">
        <f:selectItem itemValue="#{null}" itemLabel="-- select one --" />
        <f:selectItems value="#{bean.availableItems}" />
    </h:selectOneMenu>
    <h:commandButton value="Submit" action="#{bean.submit}" />
</h:form>
private String selectedItem; // +getter +setter
private List<SelectItem> availableItems; // +getter (no setter necessary)

@PostConstruct
public void init() {
    availableItems = new ArrayList<SelectItem>();
    availableItems.add(new SelectItem("foo", "Foo label"));
    availableItems.add(new SelectItem("bar", "Bar label"));
    availableItems.add(new SelectItem("baz", "Baz label"));
}

Upvotes: 2

Related Questions