0

I am implementing a multiselect autocomplete textbox (similar to one that Stackoverflow uses for tags input), using fcbkComplete. For implementation, I need to transfer JSON objects to a Jquery function from JSF. (There's a need to specify JSON url as parameter to fcbkComplete).

           $("select0").fcbkcomplete({
                   json_url: "fetched.txt"
           });

I am using JSF 2.0 with Primefaces 3.0M3. I would appreciate if someone can show me a direction as to how I can specify a JSON url to a js method in a JSF based application.

Thanks

Rajat Gupta
  • 25,853
  • 63
  • 179
  • 294

1 Answers1

0

At its simplest, you can just let a JSF managed bean getter return the desired JSON as a String and assign it to a JS variable which is inlined in the view template:

<script>
    var json = #{bean.json};
    // ...
</script>

(note that the variable value is unquoted! i.e. using var json = '#{bean.json}'; won't work out)

You can if necessary use among others Google Gson to easily convert between Java objects and JSON strings. You can if necessary put that <script> inside a <h:panelGroup> with an id so that it can be re-rendered upon an ajax request. You can if necessary end up wrapping this in a composite component.

True, it's very spartan, PrimeFaces may have better ways for approaching this, but I'm not aware of any.


As a completely different alternative, when the data does not depend on anything in the JSF context (expect of maybe some session scoped managed bean, which is available as a HttpSession attribute anyway), you can also create a simple servlet which does the job upon a jQuery.ajax() call. You can find several examples in this answer: How to use Servlets and Ajax?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • if I just set the js variable through managed bean's getter would it be possible for the JS methods to trigger further data requests after the page has been rendered !?? And also the `fcbkComplete` library methods are asking me to specify an url for the `json`s as you may see in my added code – Rajat Gupta Sep 06 '11 at 19:28
  • 1) Yes, just call the JS function after that line or during document ready. 2) Use a servlet then. – BalusC Sep 06 '11 at 19:45