如何从方法中的方法获取返回值

How to take the return value from method in a method

本文关键字:方法 获取 返回值      更新时间:2023-09-26

这里我从Javascript中的一个函数返回计数变量,如下所示:

<script  type="text/javascript">
            var count = 0;
            function resetGroupsSelector(groupId){
            //alert(groupId);
            console.log("search_report_form:"+groupId)
            //alert("search_report_form:"+groupId)
            var id = "search_report_form:"+groupId;
            //alert(count);
            if(document.getElementById(id).checked) 
                {
                count=count+1;
                alert(count);
                }
            else
                {
                count=count-1;
                alert(count);
                }
            return count;
            }
</script>

现在,我如何从JSF代码中获取计数值,以便使用htm()函数打印它?

final class GroupsSelector extends BaseRenderablePanel<GroupsSelector> {
        private GroupsSelector group(LabourInputReportCriteria.Level level) {
            HtmlSelectBooleanCheckbox box = new HtmlSelectBooleanCheckbox();
            boolean isSelected = selections.isGroupSelected(level);
            box.setSelected(isSelected);
            // box.setDisabled(isDaySelectedOnFirst(level));
            String id="groupBy" + level.getClass().getSimpleName();
            box.setId(id);
            box.setOnclick("resetGroupsSelector('"+id+"')");
            box.addValueChangeListener(u.addExpressionValueChangeListener("#{reportSearchCriteriaModel.groupBy}"));
            HtmlOutputText labelComponent = new HtmlOutputText();
            labelComponent.setValue(getGroupSelectionValue(level));
            tr().td();
            html(box);
            //html("&nbsp;");
            html(labelComponent);
            //html("<script> function resetGroupsSelector() {  var x = document.getElementById('search_report_form:groupByWeekLevel'); alert(x); } </script>");
            endTd().endTr();
            return this;
        }

请帮帮我。

谢谢,Shruthi S

您的问题不是很清楚。然而,如果您想获得一个点击计数器,即一个jsf页面被访问了多少次。您的最佳选择是使用@Singleton会话bean、jsf@ManageBean和@SessionScoped bean,如下所示:

@Singleton

public class IndexCounterBean {

private int hits = 1;
// Increment and return the number of hits
public int getHits() {
    return hits++;
}

}

然后创建一个jsf@SessionScoped@ManagedBean,让您可以访问计数器,如下所示:

@ManagedBean @SessionScoped public class IndexCount implements Serializable{

    @EJB
    private IndexCounterBean counterBean;
    private int hitCount;
    public IndexCount() {
        this.hitCount = 0;
    }
    public int getHitCount() {
        hitCount = counterBean.getHits();
        return hitCount;
    }
    public void setHitCount(int newHits) {
        this.hitCount = newHits;
    }
}

然后,从jsf(.xhtml)页面,按如下方式打印您的计数器:

<h:outputText value="#{indexCount.hitCount} Visits"/>

每次请求页面时,计数器都会递增。