首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从JSF生成JSON响应?

如何从JSF生成JSON响应?
EN

Stack Overflow用户
提问于 2012-06-11 23:06:07
回答 3查看 25.5K关注 0票数 18

我已经创建了一个页面,希望从JSF页面获得JSON响应,但当我尝试获取页面时,它会显示整个html页面。

<html xmlns="http://www.w3.org/1999/xhtml"><head>
        <title>Facelet Title</title></head><body>
[{"value": "21", "name": "Mick Jagger"},{"value": "43", "name": "Johnny Storm"},{"value": "46", "name": "Richard Hatch"},{"value": "54", "name": "Kelly Slater"},{"value": "55", "name": "Rudy Hamilton"},{"value": "79", "name": "Michael Jordan"}]

</body></html>
EN

回答 3

Stack Overflow用户

发布于 2014-07-11 17:46:21

我甚至在JSF2.2中使用文本“contentType=/xhtml”,它工作得很好。在renderJson()中不需要使用上面的BalusC答案

<f:view encoding="UTF-8" contentType="text/html"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core">
 <h:outputText value="#{stationView.getClosestStationen(param.longitude, param.latitude)}" escape="false"/>
</f:view>

Ajax调用:

        $.ajax({
            url: requestContextPath + '/rest/stationen.xhtml',
            type: "GET",
            data: {
               "longitude": x,
               "latitude": y
            },
            dataType: "json",
            success: function (data) {
              $.each(data, function (i, station) {
                 ...
              });
            },
            error: function () {
                ...
            }
        })
票数 4
EN

Stack Overflow用户

发布于 2013-07-18 17:10:41

我只使用了一个facelet作为输出(但JSF 1.2)

<f:view contentType="application/json" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">
<h:outputText value="#{someBean.getJson()}" escape="false"/>
</f:view>
票数 3
EN

Stack Overflow用户

发布于 2012-06-11 23:16:40

为什么要使用jsf呢?使用servlet为您的JSON提供服务。我的建议是使用jaxrs实现,比如cxf。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10982762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档