【问题标题】:rich:progressBar currentValue does not working with For-Loop?rich:progressBar currentValue 不能与 For-Loop 一起使用?
【发布时间】:2013-04-19 15:44:27
【问题描述】:

在 RichFaces 4.1 中,ManagedBean 中的 rich:progressBar 'currentValue' 不会使用 for 循环进行更新。

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 更新得非常快,并且突然达到 totalRecords 到 100。它没有以增量方式更新(for循环中的现值)。该方法返回的当前值不会更新进度条。

请帮忙。

【问题讨论】:

    标签: jsf richfaces progress-bar


    【解决方案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:ajaxh:commandLink 中)才能重新呈现进度条 - 在您的示例中,您在 bean 中启用了进度条,但页面上的值不会改变。

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

    有什么不清楚的地方问一下。

    【讨论】:

    • 如果我们使用 'if' 条件并增加 currentValue++,上述解决方案是有效的。但是当我们使用'for-loop'时,它不起作用。 for(currentValue = 0; currentValue
    • 如果您希望它与 for 循环一起工作,您必须在每次递增后暂停循环并让另一个方法获取当前值。有办法做到这一点,但我真的不推荐它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    相关资源
    最近更新 更多