gpt4 book ai didi

ajax - JSF PrimeFaces 进度条问题

转载 作者:行者123 更新时间:2023-12-01 06:20:06 24 4
gpt4 key购买 nike

我无法让应该是简单的 PrimeFaces 4.0 进度条工作:

我最终想做的是在我的 UI 上制作一个按钮,当我单击它时,它将启动一个长时间运行的后台任务,该任务将具有其状态,通过进度条显示。为了模拟这种情况,我将我认为是一个简单的示例放在一起(不幸的是它似乎不起作用)。

我使用了一个 ViewScoped bean,因为我认为它不应该给混合添加任何范围问题。从使用 Firebug 看来,当我点击我的 Start Function 按钮时,进度条开始发出 ajax 请求,但是,与按钮相关的 actionListener 似乎没有运行,因此我的进度永远不会更新。

有什么建议吗?

XHTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">

<f:view contentType="text/html">
<h:head>
<f:facet name="first">
<meta content='text/html; charset=UTF-8' http-equiv="Content-Type"/>
<title>PrimeFaces Progress Bar</title>
</f:facet>
</h:head>
<h:body>
<h:form>
<p:growl id="growl" />
<h3>Progress Bar Test</h3>

<p:commandButton value="Start Function" type="button"
onclick="PF('pbAjax').start();PF('startButton2').disable();"
widgetVar="startButton2"
actionListener="#{progressBean.startTestFunction}"/>

<p:commandButton value="Cancel"
actionListener="#{progressBean.cancel}"
oncomplete="pbAjax.cancel();startButton2.enable();" />

<p:progressBar widgetVar="pbAjax"
ajax="true"
value="#{progressBean.progress}"
labelTemplate="{value}%"
styleClass="animated">

<p:ajax event="complete"
listener="#{progressBean.onComplete}"
update="growl"
oncomplete="startButton2.enable()"/>
</p:progressBar>
</h:form>
</h:body>
</f:view>
</html>

bean 类

import java.io.Serializable;  
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@ViewScoped // Does the scope matter here?
public class ProgressBean implements Serializable {

public ProgressBean() {};

private Integer progress;

public Integer getProgress() {
System.out.println("getProgress: " + progress);
return progress;
}

public void startTestFunction() {
System.out.println("Staring Test Function");

for (int i = 0; i < 100; i++) {
setProgress(i);
System.out.println("TestFunction - setting progress to: " + i);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
}
}
setProgress(100);
System.out.println("Finished Function");
}

public void setProgress(Integer progress) {
System.out.println("SetProgress called: " + progress);
this.progress = progress;
}

public void onComplete() {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Progress Completed", "Progress Completed"));
}

public void cancel() {
progress = null;
}
}

最佳答案

看来我必须从命令按钮中删除 type="button" 并将其更改为:

<p:commandButton value="Start Function" 
onclick="PF('pbAjax').start();PF('startButton2').disable();"
widgetVar="startButton2"
actionListener="#{progressBean.startTestFunction}"/>

结束...:)

关于ajax - JSF PrimeFaces 进度条问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23292898/

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