Daniel Bleisteiner
Daniel Bleisteiner

Reputation: 3310

How to programmatically set <f:selectItems var> in UISelectItems instance

I've successfully replaced <s:selectItems> (Seam) with <f:selectItems> (Faces) in all of my JSPs. But I also have some Java classes using the object model and there I seem to have one unsolved problem: UISelectItems#setVar(String) doesn't seem to exist.

How can I fix my following code snippet?

ValueExpression labelExpr = expFactory.createValueExpression(elContext, "#{item.findLabel()}", String.class);

UISelectItems selectItems = new UISelectItems();
selectItems.setVar("item"); // FIXME: missing method
selectItems.setValue(new ArrayList<Element>(elements));
selectItems.setValueExpression("itemLabel", labelExpr);
return selectItems;

The JSF tag <f:selectItems value="#{items}" var="item" itemLabel="#{item.findLabel()}" /> works an supports var.

Upvotes: 1

Views: 1055

Answers (1)

BalusC
BalusC

Reputation: 1108632

If an attribute appears to have no explicit getter/setter for it, then just put it directly in the attribute map as available by UIComponent#getAttributes().

selectItems.getAttributes().put("var", "item");

Or, if it represents a value expression, then use UIComponent#setValueExpression() instead (which is inapplicable for the var attribute, by the way).


Unrelated to the concrete problem, you'd like to migrate the view-specific code from Java (the model) to XHTML (the view) as well. There's in JSF2 nothing which is impossible in XHTML and only possible in Java. Declaring/defining/creating the view in XHTML is so much more maintenance-friendly than doing so in Java.

Upvotes: 4

Related Questions