[英]JSF view calculations
我知道在JSF 2中,facelets是首選的視圖聲明語言。
是否已棄用JSP到jsf?
無論如何,我需要創建一個特殊的布局,以便不能使用Datatable。 相反,我有6個div作為列,在其中放置了文章集。 我的問題是我有一個JSF復合組件,該組件注入了Collection A:
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>
那么,如何計算這些偏移量和大小,以便每一列在集合的一部分上進行迭代? 還是有更好的方法?
您可以使用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 <fmt:formatNumber>
,它可以導出到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,您只需要將<param-name>facelets.LIBRARIES</param-name>
替換為<param-name>javax.faces.FACELETS_LIBRARIES</param-name>
。 最后,您將最終像:
<ui:param name="size" value="#{x:roundUp(fn:length(featuredArticles) / 6)}" />
作為完全不同的替代方法,您也可以在托管bean的構造函數,init或getter中完成此工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.