[英]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.