Search code examples
arraysjsfuirepeat

updating an empty integer array inside ui:repeat


I want to use ui:repeat to fill values of an integer array which is originally empty, from inputText. I followed the explanation in here, but it did't work for me. My UI is:

<ui:repeat value="#{controllerBean.schedule.scheduleTimes}" var="v" varStatus="loop">
    <h:outputText value="Shedulte Time #{loop.index + 1}">
    </h:outputText>
    <p:inputText value="#{controllerBean.schedule.scheduleTimes[loop.index]}">
    </p:inputText>
</ui:repeat>

contollerBean is the managed bean, schedule is model and scheduleTimes is Integer[] array in my model with setter and getter. After providing some values on ui, the values of the array at each index is still null.


Solution

  • You need a converter to Integer because in JSF all values are by default treated as String.

    <ui:repeat value="#{controllerBean.schedule.scheduleTimes}" var="v" varStatus="loop">
        <h:outputText value="Shedulte Time #{loop.index + 1}">
        </h:outputText>
        <p:inputText value="#{controllerBean.schedule.scheduleTimes[loop.index]}" converter="javax.faces.Integer">
        </p:inputText>
    </ui:repeat>
    

    Now the example works for me like a charm.


    My previous answer is incorrect because JSF automatically converts primitive types. A converter is however required for Collections because expected type cannot be determined at runtime.

    Nevertheless the example works with a converter or without. Therefore please make sure that controllerBean has scope other than non-scoped (without scope annotation). Otherwise the bean is recreated each time you access it.


    Well, also with request scope my code works fine. Please check the example I'm using

    I replaced primefaces p:inputText with pure JSF h:inputText but I don't think it matters. I also added some html table tags to do a formatting.

    As you can see in server logs values are set properly.