我已经创建了一个页面,希望从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>
发布于 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 () {
...
}
})
发布于 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>
发布于 2012-06-11 23:16:40
为什么要使用jsf呢?使用servlet为您的JSON提供服务。我的建议是使用jaxrs实现,比如cxf。
https://stackoverflow.com/questions/10982762
复制相似问题