Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert String into Integer in h:selectOneMenu

Tags:

jsf

jsf-2

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.

like image 648
Peter Penzov Avatar asked Sep 19 '12 15:09

Peter Penzov


People also ask

Can I convert a string to an integer?

1. Use Integer. parseInt() to Convert a String to an Integer. This method returns the string as a primitive type int.

How do I convert a string to a number?

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.

Which function converts a string to integer numbers?

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.


2 Answers

Add the Integer converter via the converter attribute.

<h:selectOneMenu converter="javax.faces.Integer"/>
like image 174
djmj Avatar answered Jan 03 '23 12:01

djmj


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.

like image 45
maple_shaft Avatar answered Jan 03 '23 13:01

maple_shaft