JSON

How to get JSON response from JSF?

字号+ 作者:H5之家 来源:H5之家 2015-11-10 16:21 我要评论( )

How to get JSON response from JSF? ,ITPUB论坛

Many JavaScript widgets expect data and options in JSON format. Nowadays, it is really easy to choose a cool widget and wrap it in a composite component. But the first question is how to send an AJAX request and to recieve a

response in a proper JSON format. This question is often raised by JSF users. All that you need is a XHTML facelet like this one:

  • 1        <f:view encoding="UTF-8" contentType="text/html"
  • 2            xmlns=""
  • 3            xmlns:h=""
  • 4            xmlns:f="">
  • 5          <h:outputText value="#{stationView.getClosestStations(param.longitude, param.latitude)}" escape="false"/>
  • 6        </f:view>

    复制代码


    Please consider the contentType=”text/html” (application/json will not work here) and escape=”false” in the hutputText. The method getClosestStations() in the bean StationView produces an JSON output for a list of special Java


    objects. I advise to use the Gson library in order to serialize any Java object to JSON. Short example:

  • 1        String[] strings = {"abc", "def", "ghi"};
  • 2        Gson gson = new Gson();
  • 3        gson.toJson(strings);  ==> prints ["abc", "def", "ghi"]

    复制代码




     

  • 1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

    相关文章
    • CentOS下网卡启动、配置等ifcfg

      CentOS下网卡启动、配置等ifcfg

      2016-02-15 16:03

    • newtonsoft.json 6.0(20141020)

      newtonsoft.json 6.0(20141020)

      2016-01-18 16:08

    • Http连接如何实现GET POST JSON数据与下载图片显示进度

      Http连接如何实现GET POST JSON数据与下载图片显示进度

      2016-01-17 18:19

    • Whats the best way to represent float64 as non

      Whats the best way to represent float64 as non

      2016-01-17 16:20

    网友点评