Jsf中进度条的用法

2023-06-09,,

Jsf中进度条用法

前端页面

 <!-- 进度条 -->
<p:progressBar widgetVar="pbAjax" ajax="true"
value="#{ProjectPackageManageBackingBean.progress}" labelTemplate="{value}%"
styleClass="animated" global="false" id="pbAjax" >
<p:ajax event="complete" listener="#{ProjectPackageManageBackingBean.onComplete}"
update=":form:message,:form:pbAjax,:form:shade" />
</p:progressBar>

后端bean层逻辑

     /** 进度条展示   **/
private Integer progress = 0; public Integer getProgress()
{
if (progress == null)
{
progress = 0;
}
try
{
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (progress == null)
{
progress = 0;
}
else
{
progress = rateCalculate(progressSum, progressDo); if (progress >= 100) progress = 100;
}
return progress;
} public void setProgress(Integer progress)
{
this.progress = progress;
}
// 进度条完成事件
public void onComplete()
{
// 进度百分比置空
progress = null;
// 总数量置空
progressSum=null;
// 选择数量置空
progressDo=null;
}
private Integer progressSum;// 总的勾选数量
private Integer progressDo;// 完成的数量 public Integer getProgressSum()
{
return progressSum;
} public void setProgressSum(Integer progressSum)
{
this.progressSum = progressSum;
} public Integer getProgressDo()
{
return progressDo;
} public void setProgressDo(Integer progressDo)
{
this.progressDo = progressDo;
}
// 计算百分比工具方法
public static Integer rateCalculate(Integer sum, Integer doSum)
{
if (sum == null)
{
return 0;
}
if (doSum == null)
{
return 0;
}
if (sum == 0 || doSum == 0)
{
return 0;
}
// 创建一个数值格式化对象
NumberFormat numberFormat = NumberFormat.getInstance();
// 设置精确到小数点后2位
numberFormat.setMaximumFractionDigits(2);
// 获取到结果
String result = numberFormat.format((float)doSum/(float)sum*100);
// 获取.出现的位置
int indexOf = result.indexOf(".");
int parseInt = 0;
// 如果没有小数则直接取值
if (indexOf==-1)
{
parseInt=Integer.parseInt(result);
}
else
{
// 截取
String substring = result.substring(0, indexOf);
// 转化
parseInt = Integer.parseInt(substring);
}
return parseInt;
}

通过计算任务的百分比,进度条会一直监听百分比,当达到百分百后,会触发进度条的完成事件,并将进度条置空,回到最初状态。

Jsf中进度条的用法的相关教程结束。

《Jsf中进度条的用法.doc》

下载本文的Word格式文档,以方便收藏与打印。