1

I am struggling hard to pass an int value to the attribute rows.

<p:dataTable rows="#{isLazy ? 25 : 1000}">
    <f:attribute name="selectionMode" value="#{widget.selectionMode}"/>
    <f:attribute name="selection" value="#{widget.selectionModel.selection}"/>
</p:dataTable>

The method from javax.faces.component.UIData that retrieves the value is defined as

public int getRows() {
    return (Integer) getStateHelper().eval(PropertyKeys.rows, 0);
}

getStateHelper().eval(PropertyKeys.rows, 0) results in a Long and thereafter can't be cast to Integer.

  1. Is there any way to tell the parser to treat the result of the expression isLazy ? 25 : 1000 as an int or Integer?
  2. Is it just me, or is that cast so poorly written?

I am new to JSF, any help would be appreciated.


UPDATE 1: Surprisingly, it works (an Integer is returned) if I exclude all the <f:attribute />s from p:dataTable. I am completely bewildered.

UPDATE 2: I've found an awful workaround. I could define a function in facelet-taglib referring to Integer.valueOf(int)

<function>
    <function-name>toInt</function-name>
    <function-class>java.lang.Integer</function-class>
    <function-signature>Integer valueOf(int)</function-signature>
</function>

and wrap up my value with it.

<f:attribute name="rows" value="#{bg:toInt(1000)}"/>
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
  • maybe instead of doing validation in p:dataTable you could call for a manage bean function? It's been a while since I played with JSF but that should work – Lukas Novicky Apr 10 '19 at 08:16
  • @LukasNovicky sure, it could be refactored. The point is I can't put even a single `int` there - it will turn it into a `Long` – Andrew Tobilko Apr 10 '19 at 08:17
  • maybe that is something typical to primeFaces? I never worked with it - always used pure JSF and had no issue with passing int, Integers nor longs of any kind. sorry I cannot help you more – Lukas Novicky Apr 10 '19 at 08:20
  • 1
    I'm bewildered there is no initial [mcve]... You mentioned the 'removal' of all `f:attribute` tags but there are none in your initial post... Please always, always, always create an [mcve]. Otherwise it is hard (next to impossible) to even try to reproduce... https://stackoverflow.com/questions/13009280/jsf-compositeattribute-with-fattribute-conversion-error – Kukeltje Apr 10 '19 at 11:19
  • @Kukeltje I had removed them when I posted it because they seemed completely irrelevant. Updated – Andrew Tobilko Apr 10 '19 at 11:22
  • That is exectly what creating an [mcve] is for... remove **really, explicitly** irrelevant code before posting. Cheers... – Kukeltje Apr 10 '19 at 11:28
  • @Kukeltje thank you, your links always make sense – Andrew Tobilko Apr 18 '19 at 08:18

1 Answers1

1

What I called "an awful workaround" turned out to be the only working solution I've found.

I defined a function in facelet-taglib that refers to Integer.valueOf(int)

<function>
    <function-name>toInt</function-name>
    <function-class>java.lang.Integer</function-class>
    <function-signature>Integer valueOf(int)</function-signature>
</function>

and could wrap up an integer literal with it.

<f:attribute name="rows" value="#{bg:toInt(1000)}"/>

That definition litters my facelet-taglib and I kinda hate it, but it works.

Helpful links:

JSF composite:attribute with f:attribute conversion error

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142