2013-05-07 37 views
0

您好,我一直在为此努力了好几个小时,我想用JSON格式将我的POJO使用Primefaces请求上下文发送到我的JSF中的Highchart以更新其值。 基本上我是从他自己的stackoverflow question 以下这个@Bhesh Gurung的解决方案,但我似乎无法使它工作。现在它是投掷:如何使用JSF2 Primefaces向Highchart发送JSON请求上下文?

Cannot find component with identifier "pieData" referenced from "j_idt31".

我想通过Primefaces请求上下文使用JSON数据成功创建一个高图。请帮助提前致谢。

这是我下面的代码

@ManagedBean 
@ViewScoped 

public class PieDataProvider { 

     public void retrievePieData() { 
      List<String> categories = new ArrayList<String>(); 
      categories.add("Electronic"); 
      categories.add("Food"); 
      categories.add("Liguor"); 
      categories.add("Stationary"); 
      categories.add("Mechanical"); 

      List<Integer> itemCounts = new ArrayList<Integer>(); 

      itemCounts.add(5); 
      itemCounts.add(20); 
      itemCounts.add(1); 
      itemCounts.add(50); 
      itemCounts.add(10); 

      RequestContext reqCtx = RequestContext.getCurrentInstance(); 
      reqCtx.addCallbackParam("categories", new Gson().toJson(categories)); 
      reqCtx.addCallbackParam("itemCounts", new Gson().toJson(itemCounts)); 
      System.out.println(categories); 
     } 

} 

我XHTML page5.xhtml

<?xml version="1.0" encoding="UTF-8"?> 

<ui:composition template="/template/common/commonLayout.xhtml"> 

    <ui:define name="content"> 
     <h:head> 

      <script type="text/javascript"> 
       src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" 
       src="http://code.highcharts.com/highcharts.js" 
       src="http://code.highcharts.com/modules/exporting.js" 
      </script> 


      <script type="text/javascript"> 
       function feedPieData(xhr, status, args) { 
        var categories = eval('(' + args.categories + ')'); 
        var itemCounts = eval('(' + args.itemCounts + ')'); 

        options.xAxis.categories = categories; 

        var series = { 
         data : [] 
        }; 

        series.name = new Date().toString(); 
        series.data = itemCounts; 

        options.series = [ series ]; 

        chart = new Highcharts.Chart(options); 
       } 
      </script> 

     </h:head> 
     <h:body> 
      <p:commandLink action="#{pieDataProvider.retrievePieData}" 
       oncomplete="feedPieData(xhr, status, args);" value="Pie chart demo" 
       update="pieData" /> 
     </h:body> 
    </ui:define> 

    <ui:define name="footer"> 
     <h2>This is page5 Footer</h2> 
    </ui:define> 

</ui:composition> 

UPDATE:改装XHTML

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:p="http://primefaces.org/ui"> 
<h:body> 
    <ui:composition template="/template/common/commonLayout.xhtml"> 
     <ui:define name="content"> 
      <h:head> 
       <script type="text/javascript" 
        src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 
       <script type="text/javascript"> 
        $(function() { 
         $('#container') 
           .highcharts(
             { 
              chart : { 
               plotBackgroundColor : null, 
               plotBorderWidth : null, 
               plotShadow : false 
              }, 
              title : { 
               text : 'Most used words, 2010' 
              }, 
              tooltip : { 
               pointFormat : '{series.name}: {point.percentage}', 
               percentageDecimals : 1 
              }, 
              plotOptions : { 
               pie : { 
                allowPointSelect : true, 
                cursor : 'pointer', 
                dataLabels : { 
                 enabled : true, 
                 color : '#000000', 
                 connectorColor : '#000000', 
                 formatter : function() { 
                  return '<b>' 
                    + this.point.name 
                    + '</b>: ' 
                    + this.percentage 
                  //+ ' %' 
                  ; 
                 } 
                } 
               } 
              }, 
              series : [ { 
               type : 'pie', 
               name : 'Browser share', 
               data : [ [ 'Red', 45.0 ], 
                 [ 'Orange', 26.8 ], { 
                  name : 'Yellow', 
                  y : 12.8, 
                  sliced : true, 
                  selected : true 
                 }, [ 'Green', 8.5 ], 
                 [ 'Blue', 6.2 ], 
                 [ 'Violet', 0.7 ] ] 
              } ] 
             }); 
        }); 
       </script> 
       <script type="text/javascript"> 
        function feedPieData(xhr, status, args) { 
         var categories = JSON.parse(args.categories); 
         var itemCounts = JSON.parse(args.itemCounts); 

         var series = { 
          data : [] 
         }; 
         options.series[0].data.length = 0; 
         series.data = categories; 
         series.data = itemCounts; 

         options.series = [ series ]; 

         chart = new Highcharts.Chart(options); 
        } 
       </script> 

       <script src="http://code.highcharts.com/highcharts.js"></script> 
       <script src="http://code.highcharts.com/modules/exporting.js"></script> 
      </h:head> 
      <h:body> 

       <div id="container" 
        style="min-width: 400px; height: 400px; margin: 0 auto"></div> 
       <h:form> 
        <p:commandLink action="#{pieDataProvider.retrievePieData}" 
         oncomplete="feedPieData(xhr, status, args);" 
         value="Pie chart demo" /> 
       </h:form> 
      </h:body> 
     </ui:define> 

     <ui:define name="footer"> 
      <h2>This is page5 Footer</h2> 
     </ui:define> 

    </ui:composition> 

</h:body> 

</html> 
+0

我有几个问题。 1.您在视图中的哪个位置有ID pieter的JSF/PF组件? 2.附件中的“”在哪里? – skuntsel 2013-05-07 08:58:23

+0

@skunstsel我想重新创建这个例子http://stackoverflow.com/questions/6032877/how-to-send-a-pojo-as-a-callback-param-using-primefaces-requestcontext 到目前为止我有没有想出我应该在xhtml中放置pieData组件的位置以及如何从链接初始化/渲染高图。 – royjavelosa 2013-05-07 09:10:46

回答

6

你似乎不明白,Highcharts是一个使用JavasScript环境运行单独的客户端上,而PrimeFaces/JSF是在服务器端和他们的行为在这种情况下简单地作为HTML,CSS和JavaScript代码生成器。

在您的特定情况下,PrimeFaces仅用于通过RequestContext将数据从服务器发送到客户端。 AJAX调用完成后,客户端会收到序列化数据,然后调用JavaScript函数,该函数根据接收的数据完全在客户端上创建一个Highcharts JS组件。

总而言之,它使我们得到以下设置。

**的视图元素**

<h:form> 
    <p:commandLink action="#{pieDataProvider.retrievePieData}" 
        oncomplete="feedPieData(xhr, status, args);" 
        value="Generate pie chart" /> 
</h:form> 

**的**的JavaScript

<script type="text/javascript"> 
    function feedPieData(xhr, status, args) { 
     var categories = JSON.parse(args.categories);//serialized data from server 
     var itemCounts = JSON.parse(args.itemCounts);//serialized data from server 

     //next you create the chart and show it 
    } 
</script> 

**的操作方法**

public void retrievePieData() { 
    List<String> categories = generateCategories(); 
    List<Integer> itemCounts = generateItems(); 
    RequestContext reqCtx = RequestContext.getCurrentInstance(); 
    reqCtx.addCallbackParam("categories", new Gson().toJson(categories));//additional serialized data to be sent 
    reqCtx.addCallbackParam("itemCounts", new Gson().toJson(itemCounts));//additional serialized data to be sent 
} 
+0

@skunstel先生我修改了我的代码并在页面加载时初始化了一张高图, 将命令按钮绕在h:form标签上,但是当我按下命令按钮时,它给我提供了 javax.faces.el.E​​valuationException:java。郎。NoClassDefFoundError:com/google/gson/Gson – royjavelosa 2013-05-07 11:51:39

+0

您需要导入[Gson](https://code.google.com/p/google-gson/)库。例如,与Maven你可以这样做: com.google.code.gson GSON 2.2.3 skuntsel 2013-05-07 12:26:01

+0

林先生知道我已经正确输入我的GSON的依赖性也有上没有错误我的PieDataProvider类。如果你愿意,我可以发布我在github上的代码,我真的要算出这个 \t \t \t com.google.code.gson \t \t \t GSON \t \t \t 2.2.2 \t \t royjavelosa 2013-05-07 12:31:18