【问题标题】:How to take the return value from method in a method如何从方法中的方法获取返回值
【发布时间】:2015-12-02 06:36:12
【问题描述】:

在这里,我从 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;
        }

请帮帮我。

谢谢, 斯鲁蒂斯

【问题讨论】:

    标签: javascript jsf


    【解决方案1】:

    你的问题不是很清楚。但是,如果您想获得点击计数器,即访问 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"/>
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-27
      • 1970-01-01
      • 2019-03-07
      • 1970-01-01
      相关资源
      最近更新 更多