gpt4 book ai didi

java - 如何在 JSF 上定期打印字符串数组

转载 作者:太空宇宙 更新时间:2023-11-04 12:32:28 25 4
gpt4 key购买 nike

我想在我的 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++;
}
}

关于java - 如何在 JSF 上定期打印字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37702325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com