Reputation: 11835
I'm trying to create a dropdown box in JSF. It needs to be filled with numbers within a given valid range. So example if the given range is between 5 and 20 than the dropdown values should be;
"5, 10 15, 20"
I have some problems, In my controller I create something like this and set values with a for loop;
List<Integer> validAmounts OR
List<String> validAmounts
Then
<myapp:selectRowElem id="autoreloadamount"
type="dropdown" selectItems="#{settingsController.validAmounts}"/>
I get error saying something like
itemValue="#{i.value}": String does not have a property 'value'
Any Ideas?
Also is there a better "JSF" way of setting the valid amounts but not creating a for loop in my controller to set the validAmounts list ?
"myapp:selectRowElem" is long but it has this for dropdown;
<h:selectOneMenu id="input" value="#{cc.attrs.item}" validatorMessage="#{msg[validatorMsgKey]}">
<f:validateRequired disabled="#{cc.attrs.forceRequired ne 'true'}"/>
<f:selectItems value="#{cc.attrs.selectItems}" var="i" itemValue="#{i.value}" itemLabel="#{i.label}" />
</h:selectOneMenu>
Upvotes: 0
Views: 3769
Reputation: 1108722
This,
<f:selectItems value="#{cc.attrs.selectItems}" var="i" itemValue="#{i.value}" itemLabel="#{i.label}" />
expects a List<SomeObject>
as #{cc.attrs.selectItems}
wherein each SomeObject
item is been assigned as loop variable #{i}
which in turn is expected to have getValue()
and getLabel()
methods returning the item value and label respectively.
Neither Integer
nor String
have those getter methods. This is also exactly what the exception is trying to tell you in case of a String
. It couldn't find the getValue()
method on it.
It appears that the #{cc.attrs.selectItems}
is expecting a List<SelectItem>
wherein the SelectItem
is the standard JSF javax.faces.model.SelectItem
object which does have those getters.
So you should be declaring and creating the list as follows:
List<SelectItem> validAmounts;
// ...
validAmounts = new ArrayList<SelectItem>();
validAmounts.add(new SelectItem(5));
validAmounts.add(new SelectItem(10));
validAmounts.add(new SelectItem(15));
validAmounts.add(new SelectItem(20));
A technically valid alternative is to create a custom class like SomeObject
with those getter methods returning the desired values. But this reinvention of the wheel is after all functionally plain clumsy and therefore not recommended.
This all by the way indicates at least 2 problems in the <portal:selectRowElem>
composite component which is apparently developed by someone else:
The <cc:attribute name="selectItems">
is missing the shortDescription
attribute which should clearly explain the enduser (you) what value exactly it expects. It should tell that it's expecting a List<SelectItem>
. This way you should immediately have noticed it during code autocomplete or by just glancing the tag documentation.
All those var
, itemLabel
and itemValue
attributes are completely superfluous. They are all the defaults already when a List<SelectItem>
is been supplied. This also indicates that the original composite component developer had no idea what he was doing.
Upvotes: 2