I have a JSF application that uses mostly Richfaces. I would like to introduce a more fea开发者_JAVA技巧ture-rich grid control, such as the jQuery-based jqgrid. Is it possible to get JSON data from a JSF backing bean? If not, is there an alternative approach?
There could be couple of options here.
Bean method can return a String which is nothing but a JSON String
public String getSearchResult(){ results = SearchService.getResults(); return JsonHelper.convertToJson(results); }
You can use JsonLib to convert Java Objects to Json.
Use a different Servlet to service Grid Requests
I will recommend this approach. Reason is you can have a JSon Request and then a JSON Response, without bothering of JSF. As i dont think when you are using jQuery you need to use JSF. So dont mix JSF in it and you can directly bypass JSF layer for this case. As it doesnt make sense here.This is the approach i used. I can share the code with you to achieve this.
Use a JSF Phase Listener to return Json Response.
Hope it helps
You could also use JAX-RS for REST services that return JSON objects. JAX-RS and JSF are both part of Java EE 6. Additionally with new JEE6 dependency injection (CDI) you can easily share session state across both JSF managed beans and JAX-RS services - a JSF managed bean can be @Inject'ed into a service class.
精彩评论