0

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.

Community
  • 1
  • 1
alem
  • 5
  • 2

1 Answers1

0

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.

Dawid Pytel
  • 2,750
  • 1
  • 23
  • 30
  • Yes, your previous answer didn't work. I am using request scope for the bean. – alem Jul 31 '14 at 07:00
  • I updated my answer with example project that I'm using. Maybe you can compare your project with mine and spot some difference. – Dawid Pytel Jul 31 '14 at 21:26
  • I appreciate your effort Dawid. Your code works fine. But, I discovered that my problem is related to the size assignment of scheduleTimes array. I want to accept its size from selectOneMenu using valueChangeListener, create the array based on this value on value change, and then use it on the ui:repeat. The problem i think is b/c the array is created after the construction of managed bean. – alem Aug 05 '14 at 14:50
  • I have changed bean to view scoped and now works. Sorry for not explaining the details... – alem Aug 05 '14 at 16:02