rich:progressBar currentValue不能与For-Loop一起使用?

时间:2013-04-19 15:44:27

标签: jsf richfaces progress-bar

在RichFaces 4.1中,来自ManagedBean的rich:progressBar'sturrentValue'不会使用for-loop进行更新。

progressBar.xhtml

 <h:form id="formProgress">
        <h:commandLink action="#{progressBarBean.startProcess}" value="click here"/>

        <rich:progressBar mode="ajax" value="#{progressBarBean.currentValue}" interval="1000" id="pb"
            enabled="#{progressBarBean.enabled}" minValue="0" maxValue="100">

            <h:outputText value="Retrieving #{progressBarBean.currentValue} of #{progressBarBean.totalRecords}" />
        </rich:progressBar>

    </h:form>

package ap;
import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class ProgressBarBean implements Serializable {

    private static final long serialVersionUID = 8775622106408411357L;


        private boolean enabled = false;

        private Integer totalRecords;
        private Integer currentValue;;

        public String startProcess() {
            setEnabled(true);
            setTotalRecords(100);
            return null;
        }

        public Integer getCurrentValue() {
            if (isEnabled()) {
                for(currentValue=0;currentValue < totalRecords;) {
                    currentValue++;
                }
            }
            return currentValue;
        }

        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }

        public Integer getTotalRecords() {
            return totalRecords;
        }

        public void setTotalRecords(Integer totalRecords) {
            this.totalRecords = totalRecords;
        }
}

当我点击“点击此处”链接时,currentValue会非常快速地更新并突然达到100的totalRecords。它没有以增量方式更新(for-loop中的当前值)。方法的当前值返回不会更新进度条。

请帮助。

1 个答案:

答案 0 :(得分:1)

有两个问题:您的Java代码没有按照您的意愿执行,并且您没有告诉页面更新(这不会自动发生)。

再次查看getCurrentValue():它会将currentValue从0增加到100并返回100的结果。#{progressBarBean.currentValue}不关心(或知道)会发生什么变量,它只关心getCurrentValue()方法的结果。

所以为了让一切顺利,它必须看起来像这样:

<a4j:commandLink action="#{progressBarBean.startProcess}" value="click here" render="pb" execute="@this"/>
    <rich:progressBar mode="ajax" value="#{progressBarBean.currentValue}" interval="1000" id="pb"
        enabled="#{progressBarBean.enabled}" minValue="0" maxValue="100">
        <a4j:ajax event="begin" listener="#{progressBarBean.increment}" render="text"/>

        <h:outputText value="Retrieving #{progressBarBean.currentValue} of #{progressBarBean.totalRecords}" id="text" />
    </rich:progressBar>

a4j:ajax每秒触发一次(即每个间隔),它会递增currentValue并更新文本。

您还需要a4j:commandLink(或a4j:ajax中的h:commandLink才能重新呈现进度条 - 在您的示例中,您在bean中启用了进度条,但页面上的值却是不要改变。

public Integer getCurrentValue() {
    return currentValue;
}

public void increment() {
    if (isEnabled() && currentValue < totalRecords) {
        currentValue++;
    }
}

询问是否有任何不清楚的事。