Pablo Escobarl
Pablo Escobarl

Reputation: 11

What's the difference between using ui:repeat and c:forEach, and when to using them?

I'm studying/working with JSF 2.1, landed on a project started back in 2013 or something. There are no composite components exc. around the project, a lot of doubled code which can be optimized with the JSF features.

I'm having some problem iterating over a Map, I think that I reach the code that I want to trigger but something goes wrong throwing a NullPointerException and I think it's the way that I handled the implementation.

IMPORTANT: I'm not using a backing bean for the component. Instead, we use one for the pages under the same directory, so reusing this component around the project means that I can't add for every directory's bean specific things for every composite component I'll create, I definetely don't think could be a good choice for clean code purposes. Maybe I'll refactor something, but for the moment I don't want to break anything.

I implemented a composite button as follows:

<cc:attribute name="myMap" type="java.util.Map" required="false"/>

<h:commandButton {...otherStuff}>
<h:panelGroup rendered="#{cc.attrs.myMap != null and not empty cc.attrs.myMap}">
    <ui:repeat var="param" value="#{cc.attrs.additionalParams.entrySet().toArray()}">
        <f:param name="#{param.key}" value="#{param.value}" />
    </ui:repeat>
</h:panelGroup>
</h:commandButton>

And the usage:

<component:button {...otherStuff} myMap="#{{'key':'value'}}"/>

I got the implementation without the composite component (so, static) that has a behaviour, this one not. I'm not expecting an exception since teorically I don't see anything wrong.

I know c:forEach is evaluated at build time, ui:repeat at runtime, but the naming is very confusing. There's no UI to repeat, just attributes and in the beginning I was using c:forEach, but I had the same problem basically.

To have the greater picture, this component works perfectly when that attribute is not specified.

Upvotes: 0

Views: 21

Answers (0)

Related Questions