Jsf中進度條的用法前端
前端頁面git
1 <!-- 進度條 --> 2 <p:progressBar widgetVar="pbAjax" ajax="true" 3 value="#{ProjectPackageManageBackingBean.progress}" labelTemplate="{value}%" 4 styleClass="animated" global="false" id="pbAjax" > 5 <p:ajax event="complete" listener="#{ProjectPackageManageBackingBean.onComplete}" 6 update=":form:message,:form:pbAjax,:form:shade" /> 7 </p:progressBar>
後端bean層邏輯ajax
1 /** 進度條展現 **/ 2 private Integer progress = 0; 3 4 public Integer getProgress() 5 { 6 if (progress == null) 7 { 8 progress = 0; 9 } 10 try 11 { 12 Thread.sleep(1000); 13 } catch (InterruptedException e) { 14 e.printStackTrace(); 15 } 16 if (progress == null) 17 { 18 progress = 0; 19 } 20 else 21 { 22 progress = rateCalculate(progressSum, progressDo); 23 24 if (progress >= 100) 25 26 progress = 100; 27 } 28 return progress; 29 } 30 31 public void setProgress(Integer progress) 32 { 33 this.progress = progress; 34 } 35 // 進度條完成事件 36 public void onComplete() 37 { 38 // 進度百分比置空 39 progress = null; 40 // 總數量置空 41 progressSum=null; 42 // 選擇數量置空 43 progressDo=null; 44 } 45 private Integer progressSum;// 總的勾選數量 46 private Integer progressDo;// 完成的數量 47 48 public Integer getProgressSum() 49 { 50 return progressSum; 51 } 52 53 public void setProgressSum(Integer progressSum) 54 { 55 this.progressSum = progressSum; 56 } 57 58 public Integer getProgressDo() 59 { 60 return progressDo; 61 } 62 63 public void setProgressDo(Integer progressDo) 64 { 65 this.progressDo = progressDo; 66 } 67 // 計算百分比工具方法 68 public static Integer rateCalculate(Integer sum, Integer doSum) 69 { 70 if (sum == null) 71 { 72 return 0; 73 } 74 if (doSum == null) 75 { 76 return 0; 77 } 78 if (sum == 0 || doSum == 0) 79 { 80 return 0; 81 } 82 // 建立一個數值格式化對象 83 NumberFormat numberFormat = NumberFormat.getInstance(); 84 // 設置精確到小數點後2位 85 numberFormat.setMaximumFractionDigits(2); 86 // 獲取到結果 87 String result = numberFormat.format((float)doSum/(float)sum*100); 88 // 獲取.出現的位置 89 int indexOf = result.indexOf("."); 90 int parseInt = 0; 91 // 若是沒有小數則直接取值 92 if (indexOf==-1) 93 { 94 parseInt=Integer.parseInt(result); 95 } 96 else 97 { 98 // 截取 99 String substring = result.substring(0, indexOf); 100 // 轉化 101 parseInt = Integer.parseInt(substring); 102 } 103 return parseInt; 104 }
經過計算任務的百分比,進度條會一直監聽百分比,當達到百分百後,會觸發進度條的完成事件,並將進度條置空,回到最初狀態。後端