0

The value in f:selectItem is an Integer (Const.DB_NEW_DATASET) but the output of testlistener is always java.lang.String. That's not what I had expected.

xhtml

<f:metadata>
    <f:importConstants type="foo.bar.Const" />
</f:metadata>
<h:selectOneListbox value="#{viewScope.foo}">
    <f:selectItem
        itemValue="#{Const.DB_NEW_DATASET}"
        itemLabel="foo" />
    <f:selectItem
        itemValue="#{Const.DB_NEW_DATASET}"
        itemLabel="bar" />
    <f:ajax listener="#{myBean.testlistener}" />
</h:selectOneListbox>

bean

@Named
@ViewScoped
public class MyBean implements Serializable {
    @Inject
    @ViewMap
    private Map<String, Object> viewMap;

    public void testlistener() {
        System.out.println(viewMap.get('foo').getClass());
    }
}


public class Const {
    public static final Integer DB_NEW_DATASET = -1;
}

Mojarra 2.3.9.SP01

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Toru
  • 905
  • 1
  • 9
  • 28

1 Answers1

2

It's actually not "cast to java.lang.String" at all. It's just the default type of ServletRequest#getParameter() which is then left unconverted by JSF.

This is because the ValueExpression#getType() of #{viewScope.foo} returns java.lang.Object and thus JSF won't perform any automatic conversion against any registered @FacesConverter(forClass).

You need to explicitly specify the built-in javax.faces.Integer converter which is essentially a @FacesConverter(forClass=java.lang.Integer).

<h:selectOneListbox ... converterId="javax.faces.Integer">

This is not necessary if you're using e.g. #{bean.foo} with a private Integer foo, because this way the ValueExpression#getType() will return java.lang.Integer and thus JSF can find the built-in converter.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555