JSF 视图计算

发布于 2024-10-16 15:12:10 字数 1467 浏览 1 评论 0原文

我知道对于 JSF 2,facelets 是首选的视图声明语言。

JSP 到 jsf 是否已弃用?

无论如何,我需要创建一个特殊的布局,所以我不能使用数据表。相反,我使用 6 个 div 作为列,在其中放置文章集合。 我的问题是我有一个 JSF 复合组件,它注入了一个 Collection 答:

List<Article>

对象。

然后,该组件需要将集合的大小划分为每列的相等部分。然后为每个列设置适当的偏移量和大小

<ui:repeat></ui:repeat> 

,这样我最终会得到这个

<!-- 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>

那么我如何计算这些偏移量和大小,以便每列迭代集合的一部分?或者也许有更好的方法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见终念 2024-10-23 15:12:10

您可以使用fn:length获取集合的大小,并且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>

更新:至于舍入,这会变得棘手。在旧的 JSP 中,您可以使用 JSTL 来实现此目的,它可以导出到 var 属性,而不是直接在视图中显示它。

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

但 JSTL fmt 在 Facelets 中不可用。

一种巧妙的方法是使用 fn:substringBefore 来分割分数。

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

但这总是向下舍入。

最好的方法是创建自定义 EL 函数。您可以在 这个答案。对于 JSF 2.0,您只需将已弃用的 facelets.LIBRARIES 替换为 javax.faces.FACELETS_LIBRARIES。最后,您将得到如下结果:

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

作为完全不同的替代方案,您还可以在托管 bean 的构造函数、init 或 getter 中完成这项工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文