JSF 视图计算
我知道对于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
fn:length
获取集合的大小,并且EL中有基本的算术运算符。更新:至于舍入,这会变得棘手。在旧的 JSP 中,您可以使用 JSTL
来实现此目的,它可以导出到var
属性,而不是直接在视图中显示它。但 JSTL
fmt
在 Facelets 中不可用。一种巧妙的方法是使用 fn:substringBefore 来分割分数。
但这总是向下舍入。
最好的方法是创建自定义 EL 函数。您可以在 这个答案。对于 JSF 2.0,您只需将已弃用的
facelets.LIBRARIES
替换为javax.faces.FACELETS_LIBRARIES
。最后,您将得到如下结果:作为完全不同的替代方案,您还可以在托管 bean 的构造函数、init 或 getter 中完成这项工作。
You can get collection's size with
fn:length
and there are basic arithmetic operators in EL.Update: as to the rounding, that get tricky. In old JSP you could use JSTL
<fmt:formatNumber>
for this which can export to avar
attribute instead of displaying it straight in the view.But the JSTL
fmt
is not available in Facelets.A hacky way would be to split the fractions using
fn:substringBefore
.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:As a completely different alternative, you could also do this job in the constructor, init or getter of a managed bean.