How to send POJO as a callback parameter using RequestContext PrimeFaces?

I can send callback parameters, and it works fine so far, while I only send some primitive types like String. But the same does not work even for the simplest POJO. The PrimeFaces manual says that the RequestContext.addCallbackParam () method can handle POJOs and it hides them in JSON. I do not know why it does not work in my case.

Has anyone done this?

+6
source share
1 answer

Solution found! -------------------------------------------------- -------------------

I did some research and found the answer to this question.

And there was a decision to use some JSON library (I am using GSON now) to convert Java objects to JSON objects.

new Gson (). toJson (someJavaObj)

returns a string. Just send the string as a parameter and on the client side, using js' eval or some js library function to include this again in JSON.

Actually, it was pretty simple and clean.

Sorry, I have not actually posted the solution. Below is my solution -

Bean backup action method is

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)); } 

PrimeFaces p: commandButton in view -

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

Javascript Function -

 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); } 

I would greatly appreciate and welcome any suggestion or opinion. Thanks!

+6
source

Source: https://habr.com/ru/post/888330/


All Articles