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

时间:2015-12-02 06:36:13

标签: javascript jsf

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

<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

1 个答案:

答案 0 :(得分:0)

你的问题不是很清楚。但是,如果你想得到一个命中计数器,即有多少次访问过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"/>

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