将JSON文本从浏览器发送到JSF托管Bean(Primefaces) [英] Sending JSON text from Browser to JSF Managed Bean (Primefaces)
问题描述
我正在开发一个应用程序,正在考虑将信息以JSON格式发送回浏览器,并让Java脚本根据该信息呈现页面.我想以同样的方式将更新从浏览器以JSON格式推送到托管bean.我们可以在JSF + Primefaces中完成此任务吗?
I am working on a app where I am thinking of sending information back to the browser in JSON format and have java script render the page based on the information. In the same fashion, I want to push updates from the browser to the managed bean in JSON format. Can we accomplish this in JSF + Primefaces?
我们考虑使用JSON的原因是为了限制来回传递的数据量.
The reason we are thinking of using JSON is to limit the amount of data being passed back and forth.
推荐答案
我可以推荐使用Gson Gson用户指南
I can recommend on using Gson Gson User Guide
这是一个简短示例,说明如何从服务器创建/发送/解析json
Here is a brief example of how create/send/parse json from server
在您的bean中做这样的事情
in your bean do something like this
Gson gson = new Gson();
myJsonString = gson.toJson(someObjectOrListOfObjects);
在xhtml中
<h:inputHidden id="my_json_data" value="#{myBean.someObjectOrListOfObjects}" />
在您的js文件中
var data = $('input[id$="my_json_data"]').val();
data = jQuery.parseJSON(data); //to parse it, use dev tools/fire bug to add data variable to watch and observe it
这篇关于将JSON文本从浏览器发送到JSF托管Bean(Primefaces)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!