问题描述
如何使用 only 标准标记(ui:,h:等)在JSF中重复输出某些内容?换句话说-如何等效于下面的JSF中的PHP代码?我立即想利用ui:repeat
的优势,但它需要收集-我只有数字.
How to repeat output of some content in JSF using only standard tags (ui:, h: etc) ? In other words - how to do equivalent to PHP code below in JSF ? I immediately wanted to take advantage of ui:repeat
, but it needs collection - I have only number.
for ($i = 0; $i < 10; $i++) {
echo "<div>content</div>";
}
推荐答案
都使用 <c:forEach>
(确实,有时不赞成将JSTL与JSF混合使用,但这在您的特定情况下不会造成损害,因为您似乎想要静态"创建视图;它不依赖于任何动态变量):
Either use <c:forEach>
instead (true, mixing JSTL with JSF is sometimes frowned upon, but this should not harm in your particular case because you seem to want to create the view "statically"; it does not depend on any dynamic variables):
xmlns:c="http://java.sun.com/jsp/jstl/core"
...
<c:forEach begin="1" end="10">
<div>content</div>
</c:forEach>
Or 创建一个EL函数来为<ui:repeat>
创建一个虚拟数组:
Or create an EL function to create a dummy array for <ui:repeat>
:
package com.example.util;
public final class Functions {
private Functions() {
//
}
public static Object[] createArray(int size) {
return new Object[size];
}
}
在/WEB-INF/util.taglib.xml
中注册的
:
which is registered in /WEB-INF/util.taglib.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"
version="2.0">
<namespace>http://example.com/util/functions</namespace>
<function>
<function-name>createArray</function-name>
<function-class>com.example.util.Functions</function-class>
<function-signature>Object[] createArray(int)</function-signature>
</function>
</facelet-taglib>
,其用途如下
xmlns:util="http://example.com/util/functions"
...
<ui:repeat value="#{util:createArray(10)}">
<div>content</div>
</ui:repeat>
更新:我刚刚发布了一个增强请求,将begin
和end
属性添加到<ui:repeat>
: http://java.net/jira/browse/JAVASERVERFACES-2240
Update: I just posted an enhancement request to add the begin
and end
attributes to <ui:repeat>
: http://java.net/jira/browse/JAVASERVERFACES-2240
更新2 :我已经按照 https://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-1102 从Mojarra 2.3-m06开始,您必须能够使用
Update 2: I have personally implemented it for JSF 2.3 as per https://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-1102 Since Mojarra 2.3-m06 you must be able to use
<ui:repeat begin="1" end="10">
<div>content</div>
</ui:repeat>
这篇关于在没有模型的情况下,如何通过简单的for循环重复文本输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!