Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 如何在h:SelectOne菜单中将字符串转换为整数_Jsf_Jsf 2 - Fatal编程技术网

Jsf 如何在h:SelectOne菜单中将字符串转换为整数

Jsf 如何在h:SelectOne菜单中将字符串转换为整数,jsf,jsf-2,Jsf,Jsf 2,我想在JSF hLselectOneMenu中将字符串转换为整数 <h:selectOneMenu id="rowsPerPage" value="#{AccountsController.rowsPerPage}" converter="javax.faces.Integer" maxlength="3"> <f:selectItem itemValue="10" itemLabel="10" /

我想在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" />

如何在JSF页面中实现这一点


另外,我更新了代码,但是AJAX代码不起作用。当我选择“自定义”时,输入字段不会呈现。

这里有一些问题

  • 组件的
    h:selectOneMenu
    没有名为
    size
    的属性。去掉这个

  • 无法将选择项
    custom
    解析为整数,因此只要此选择项存在,就会出现错误

  • 您需要在
    h:selectOneMenu
    组件中添加数字转换器<代码>


解决这些问题后,将组件的值绑定到整数类型的托管属性将不会有问题。

通过converter属性添加整数转换器

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


+1表示h上没有大小属性:selectOneMenu。我在将h:inputText转换为h:selectOneMenu时遇到了同样的问题。注意,这不适用于PrimeFaces
。除此之外,组件似乎根本不需要这个。(仅适用于谷歌搜索此项的用户)@Kawu大多数情况下,该组件可以毫无问题地检测绑定值的类型。在某些情况下,这是行不通的——例如泛型的用法:在编译时,已知有一个整数,但在字节码中它是一个对象。这里这个解决方案很有帮助!