Peter Penzov
Peter Penzov

Reputation: 1754

How to convert String into Integer in h:selectOneMenu

I want to convert String into Integer in JSF hLselectOneMenu.

<h:selectOneMenu id="rowsPerPage" value="#{AccountsController.rowsPerPage}" converter="javax.faces.Integer" maxlength="3">                                    
    <f:selectItem itemValue="10" itemLabel="10" />
    <f:selectItem itemValue="50" itemLabel="50" />
    <f:selectItem itemValue="100" itemLabel="100" />
    <f:selectItem itemValue="500" itemLabel="500" />                                    
    <f:selectItem itemValue="094332" itemLabel="Custom" />
    <f:ajax render="customrowperpage" />
</h:selectOneMenu>&nbsp;
    <h:inputText id="customrowperpage" value="#{AccountsController.rowsPerPage}" rendered="#{AccountsController.rowsPerPage == '094332'}" required="true" />

How I can do this in JSF page?

P.S I updated the code but it the AJAX code is not working. When I select "custom" the input field is not rendered.

Upvotes: 7

Views: 27212

Answers (2)

djmj
djmj

Reputation: 5554

Add the Integer converter via the converter attribute.

<h:selectOneMenu converter="javax.faces.Integer"/>

Upvotes: 17

maple_shaft
maple_shaft

Reputation: 10463

There are a few issues here.

  • There is no attribute named size of the component h:selectOneMenu. Remove this.

  • The select item custom cannot be resolved to an integer, so as long as this select item exists you will get errors.

  • You need to add a number converter to your h:selectOneMenu component. <f:convertNumber integerOnly="true" />

Once you resolve these then there will be no problem binding the value of the component to a managed property that is an Integer type.

Upvotes: 5

Related Questions