如何从 JSF 生成 JSON 响应?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10982762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:22:50  来源:igfitidea点击:

How to generate JSON response from JSF?

jsonjsf

提问by Jitendra

I have created a page where I want to get the JSON response from a JSF page, but when i try to get page it shows me whole html page.

我创建了一个页面,我想在其中从 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>

回答by BalusC

JSF is a MVC framework generating HTML, not some kind of a REST web service framework. You're essentially abusing JSF as a web service. Your concrete problem is simply caused by placing <html>tags and so on in the view file yourself.

JSF 是一个生成 HTML 的 MVC 框架,而不是某种 REST Web 服务框架。您实际上是在滥用 JSF 作为 Web 服务。您的具体问题只是由您自己<html>在视图文件中放置标签等引起的。

If you really insist, then you can always achieve this by using <ui:composition>instead of <html>. You also need to make sure that the right content type of application/jsonis been used, this defaults in JSF namely to text/html.

如果您真的坚持,那么您始终可以通过使用<ui:composition>代替<html>. 您还需要确保使用了正确的内容类型application/json,这在 JSF 中默认为text/html

<ui:composition
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    <f:event type="preRenderView" listener="#{bean.renderJson}" />
</ui:composition>

with

public void renderJson() throws IOException {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    externalContext.setResponseContentType("application/json");
    externalContext.setResponseCharacterEncoding("UTF-8");
    externalContext.getResponseOutputWriter().write(someJsonString);
    facesContext.responseComplete();
}

But I strongly recommend to look at JAX-RS or JAX-WS instead of abusing JSF as a JSON web service. Use the right tool for the job.

但我强烈建议查看 JAX-RS 或 JAX-WS,而不是将 JSF 滥用为 JSON Web 服务。为工作使用正确的工具。

See also:

也可以看看:

回答by Oleg

I'm even using contentType="text/xhtml" with JSF 2.2 and it works great. No needs in renderJson() from the BalusC's answer above

我什至在 JSF 2.2 中使用 contentType="text/xhtml" 并且效果很好。上面 BalusC 的回答中不需要 renderJson()

<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 call:

阿贾克斯调用:

        $.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 () {
                ...
            }
        })

回答by webstrap

I used just a facelet for the output (but JSF 1.2)

我只使用了一个 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>

回答by Joeri Hendrickx

Why are you using jsf for that? Use a servlet for serving your JSON. My suggestion would be to use a jaxrs implementation, like cxf.

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