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>
<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.
1. Use Integer. parseInt() to Convert a String to an Integer. This method returns the string as a primitive type int.
You convert a string to a number by calling the Parse or TryParse method found on numeric types ( int , long , double , and so on), or by using methods in the System. Convert class. It's slightly more efficient and straightforward to call a TryParse method (for example, int.
In JavaScript parseInt() function (or a method) is used to convert the passed in string parameter or value to an integer value itself. This function returns an integer of base which is specified in second argument of parseInt() function.
Add the Integer converter via the converter attribute.
<h:selectOneMenu converter="javax.faces.Integer"/>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With