如何将JSF Bean数据传递给JavaScript函数?

时间:2013-08-12 10:45:20

标签: javascript jsf primefaces

我使用以下代码传递数据:

<p:poll interval="3" listener="#{chartController.loadChartData}" oncomplete="renderChart('container','area','Sample Chart', '#{chartController.chartData}', '#{chartController.categories}');" 
                      id="chartvalue_btn" update="textID" />

但是当我检查null值时,我在JavaScript函数中传递了值,同时我检查了用值初始化的bean变量。我在这里做错了吗? 和我的豆是

package com.bchetty.charts.controller;

import com.bchetty.charts.model.Series;
import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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

/**
 * Chart Controller
 */
@ManagedBean(name="chartController")
@SessionScoped
public class ChartController {
    private String chartData;
    private String categories;
    private List<String> categoryList = new ArrayList<String>();
    private List<Long> heapSizeList = new ArrayList<Long>();
    private List<Long> usedHeapSizeList = new ArrayList<Long>();
    SimpleDateFormat sdfDate = new SimpleDateFormat("HH:mm:ss");//dd/MM/yyyy
    private static final long MB = 1024*1024;
    int index = 0;
    private Long[] longs;

    /**
     * Load Chart Data
     */
    public void loadChartData() {
        if(heapSizeList.size() > 10) {
            heapSizeList.remove(0);
            usedHeapSizeList.remove(0);
            categoryList.remove(0);
        }
        List<Series> series = new ArrayList<Series>();

        malloc();
        long heapSize = Runtime.getRuntime().maxMemory();
        heapSizeList.add(heapSize/MB);
        usedHeapSizeList.add((heapSize - Runtime.getRuntime().freeMemory())/MB);

        series.add(new Series("Heap Size", heapSizeList));
        series.add(new Series("Used Heap", usedHeapSizeList));

        setChartData(new Gson().toJson(series));

        categoryList.add(sdfDate.format(new Date()));
        System.out.println(categoryList);

        setCategories(new Gson().toJson(categoryList));
    }

    /**
     * @return the chartData
     */
    public String getChartData() {
        return chartData;
    }

    /**
     * @param chartData the chartData to set
     */
    public void setChartData(String chartData) {
        this.chartData = chartData;
    }

    /**
     * @return the categories
     */
    public String getCategories() {
        return categories;
    }

    /**
     * @param categories the categories to set
     */
    public void setCategories(String categories) {
        this.categories = categories;
    }

    private void malloc() {
        if(index%2 == 0) {
            longs = new Long[100000];
            for(int i=0;i<1000;i++) {
                longs[i] = Long.valueOf(i);
            }
        } else {
            longs = null;
        }
        index++;
    }
}

1 个答案:

答案 0 :(得分:2)

问题是oncomplete - 函数的参数仅在输入视图时更新。当bean上的属性值发生更改时,参数不会自动更新。因此,当chartController.chartData最初没有实例化时,在呈现页面时它是null并且在您手动刷新整个页面之前保持null

要解决此问题,我建议您使用Primefaces RequestContext,并在每次调用chartData作为回调参数时添加loadChartData。 即将以下内容添加到loadChartData

public void loadChartData() {
    ...
    RequestContext context = RequestContext.getCurrentInstance();
    context.addCallbackParam("chartData", chartData);
    context.addCallbackParam("categories", categories);
}

在UI中定义p:poll如下:

<p:poll interval="3" listener="#{chartController.loadChartData}"
    oncomplete="renderChart('container','area','Sample Chart', args.chartData, args.categories);"
    id="chartvalue_btn" update="textID" />

如果您没有选择,因为您在其他地方使用loadChartData,请将loadChartData的调用与addCallbackParam内容包装在另一个方法中,并将此方法指定为侦听器p:poll