我想在我的 JSF 页面上定期打印一个字符串数组,但我无法实现这一点。 JSF 仅一次打印整个数组,而不是定期打印。
这是我的 XHTML 代码:
<h:form>
<h:outputText id="eventener" value="#{eventBean.names}" />
<p:poll interval="2" listener="#{eventBean.names}" update="eventener" />
</h:form>
这是我的 bean :
@ManagedBean
@ViewScoped
public class EventBean implements Serializable {
private List<String> names;
@PostConstruct
public void namesInterval() {
names = Arrays.asList("Steve is late for work", "Segun is coming back next week", "Tope is a beautiful girl");
}
public List<String> getNames() {
return names;
}
}
我不明白你想做什么。您想一次打印数组中的每个项目吗?当您更新(添加项目)数组时?
您随时都不会更新数组,民意调查正在按预期进行。在您的 listener 属性上,您每 2 秒设置一次所需的内容,例如从外部源刷新您的数组。如果您尝试每 2 秒从数组中打印一个项目,您可以尝试这样的操作
<h:form>
<h:outputText id="eventener" value="#{eventBean.item}" />
<p:poll interval="2" listener="#{eventBean.pollListener}" update="eventener" />
</h:form>
bean
@ManagedBean
@ViewScoped
public class EventBean implements Serializable {
private List<String> names;
private String item;
private int count;
@PostConstruct
public void namesInterval() {
names = Arrays.asList("Steve is late for work", "Segun is coming back next week", "Tope is a beautiful girl");
count = 0;
}
public List<String> getNames() {
return names;
}
public String getItem(){
return item;
}
public void pollListener(){
//you can use an iterator too
if (count >= names.size())
{
count = 0;
}
item = names.get(count);
count++;
}
}
我是一名优秀的程序员,十分优秀!