eclipse-ee4j / mojarra

Mojarra, a Jakarta Faces implementation
Other
158 stars 107 forks source link

MenuRenderer - ClassCastException on get("size") if not a Select...Listbox #5431

Closed shadogray closed 2 months ago

shadogray commented 3 months ago

MenuRenderer tries to access "size" attribute even if not rendering Listbox. Because on unchecked cast to Integer it will cause a ClassCastException The attribute "size" is not forbidden for Select...Menu.

Example:

    <h:selectOneMenu size="3" value="test" >
        <f:selectItem noSelectionOption="true" itemLabel="..."/>
        <f:selectItem itemLabel="Name1" itemValue="name1"/>
        <f:selectItem itemLabel="Name2" itemValue="name2"/>
    </h:selectOneMenu>

Will cause:

 SEVERE [jakarta.enterprise.resource.webcontainer.faces.application] (default task-1) Error Rendering View[/test.xhtml]: java.lang.ClassCastException: class java.lang.String cannot be cast to class java.la
ng.Integer (java.lang.String and java.lang.Integer are in module java.base of loader 'bootstrap')
        at jakarta.faces.impl@4.0.4//com.sun.faces.renderkit.html_basic.MenuRenderer.renderSelect(MenuRenderer.java:650)
        at jakarta.faces.impl@4.0.4//com.sun.faces.renderkit.html_basic.MenuRenderer.encodeEnd(MenuRenderer.java:189)
        at jakarta.faces.impl@4.0.4//jakarta.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:585)

Found in: Mojarra 4.0.4 according master bug still exists

BalusC commented 3 months ago

Nice find. Work around: use a bean property returning int.

Note: noSelectionOption="true" is useless without hideNoSelectionOption="true". You probably wanted to use itemValue="#{null}" instead. See also https://stackoverflow.com/q/11360030