1

I know that with JSF 2, facelets is the preferred view declaration language.

Is JSP to jsf deprecated?

Anyway, I need to create a special layout so I cannot use Datatable. Instead, I have 6 divs that I use as columns in which I drop a collection of Articles. My problem is that I have a JSF composite component, that is injected with a Collection A:

List<Article>

object.

The component then needs to divide the size of the collection into equal pieces for each column. Then set the appropiate offset and size for each

<ui:repeat></ui:repeat> 

so i end up with this

<!-- INTERFACE -->
<cc:interface>
    <cc:attribute name="featuredArticles" required="true" type="java.util.List;" />
</cc:interface>

<!-- IMPLEMENTATION -->
  <cc:implementation>
    <div class="col">
       <ui:repeat value="#{cc.attrs.featuredArticles}" var="art" offset="??" size="??">
            <mycomps:article art="#{art}" />
       </ui:repeat>
    </div>
    <div class="col">
       <ui:repeat value="#{cc.attrs.featuredArticles}" var="art" offset="??" size="??">
            <mycomps:article art="#{art}" />
       </ui:repeat>
    </div>
    <div class="col">
       <ui:repeat value="#{cc.attrs.featuredArticles}" var="art" offset="??" size="??">
            <mycomps:article art="#{art}" />
       </ui:repeat>
    </div>
    <div class="col">
       ...same here...
    </div>
    <div class="col">
       ...same here...
    </div>
</cc:implementation>

So how do I calculate those offsets and sizes so that each columns iterates over a portion of the collection? Or maybe there's a better way?

arg20
  • 4,893
  • 1
  • 50
  • 74
  • The facelet technology works better than JSP for JSF. – Thorbjørn Ravn Andersen Feb 06 '11 at 10:12
  • To answer the first part of your question, in JSF 2.0, JSP is deprecated. From wikipedia: 'Initially, Facelets was available as a separate, alternative view declaration language for JSF 1.1 and JSF 1.2 which both used JSP as the default view declaration language. Starting from JSF 2.0, Facelets has been promoted by the JSF expert group to be the default view declaration language. JSP has been deprecated as a legacy fall back [2] [3]'. – Arjan Tijms Feb 06 '11 at 14:07
  • does that mean that at some point jsp will be completely deprecated? It states that it has been deprecated as jsf's default VDL, but if it is replaced by facelets should it be deprecated entirely? – arg20 Feb 08 '11 at 06:50
  • If by "completely deprecated" you mean "removed" then no, that's never going to happen, at least judging from experience with deprecated parts from the standard API. It may well get fewer/slower bugfixes, less documentation and may not be extended to support new features. – Michael Borgwardt Feb 08 '11 at 12:33

1 Answers1

3

You can get collection's size with fn:length and there are basic arithmetic operators in EL.

<ui:composition xmlns:fn="http://java.sun.com/jsp/jstl/core">
    ...
    <ui:param name="size" value="#{fn:length(featuredArticles) / 6}" />
    ...
    <ui:repeat size="#{size}">
    ...
</ui:composition>

Update: as to the rounding, that get tricky. In old JSP you could use JSTL <fmt:formatNumber> for this which can export to a var attribute instead of displaying it straight in the view.

<fmt:formatNumber var="size" value="${fn:length(featuredArticles) / 6}" pattern="0" />

But the JSTL fmt is not available in Facelets.

A hacky way would be to split the fractions using fn:substringBefore.

<ui:param name="size" value="#{fn:substringBefore(fn:length(featuredArticles) / 6, '.')}" />

But this always rounds down.

The best way would be to create a custom EL function. You can find an example in this answer. For JSF 2.0 you only need to replace the deprecated <param-name>facelets.LIBRARIES</param-name> by <param-name>javax.faces.FACELETS_LIBRARIES</param-name>. Finally you'll end up like as:

<ui:param name="size" value="#{x:roundUp(fn:length(featuredArticles) / 6)}" />

As a completely different alternative, you could also do this job in the constructor, init or getter of a managed bean.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • BalusC man you know everything. Is there anyway I can round up the division? It returns a float value plus if it's not a round number I can't tell to iterate 3.7 times. – arg20 Feb 08 '11 at 05:25