我有一个实现JSON查询服务的SlingServlet(非常类似于AEM Query Builder servlet)。它创建一个SyntheticResource来响应JSP" view&#34 ;,这是要服务的JSON。此JSP使用a来包含每个结果对象,它们是cq:Page nodes。
当尝试包含无法渲染的节点时,会导致400错误,但是会以一种特殊的方式。首先出现错误,然后是预期的JSON(格式不正确,因为缺少了无法包含的值)。我可以覆盖400错误文本,以便返回" null",但我无法将错误显示为内联。
考虑:
response.api.json.jsp
{
"metadata": {
"page": ${pageIndex},
},
"data": [
<%
String[] results = (String[])slingRequest.getAttribute("results");
int i = 0;
if (results != null) {
for (String path : results) {
if (i > 0) {
%>
,
<%
}
%>
<sling:include path="<%=path%>"/>
<%
i++;
}
}
%>
]
}
当前回复
<400.jsp error message up here>
{
"metadata": {
"page": 0,
},
"data": [
, // <- failed to include the path
{
"key1": , // <- failed to include the value string/object/etc
"key2": "value2"
}
]
}
预期回复
{
"metadata": {
"page": 0,
},
"data": [
<400.jsp error message>, // <- failed to include the path
{
"key1": <400.jsp error message>, // <- failed to include the value string/object/etc
"key2": "value2"
}
]
}
预期回复(假设400.jsp仅包含&#34; null&#34;)
{
"metadata": {
"page": 0,
},
"data": [
null, // <- failed to include the path
{
"key1": null, // <- failed to include the value string/object/etc
"key2": "value2"
}
]
}
有没有办法让错误页面(400.jsp)显示为内联,以便我可以返回null?
response.api.json.jsp (主要&#34;查看&#34;服务响应)
{
"metadata": {
"page": ${pageIndex},
},
"data": [
<%
String[] results = (String[])slingRequest.getAttribute("results");
int i = 0;
if (results != null) {
for (String path : results) {
if (i > 0) {
%>
,
<%
}
%>
<sling:include path="<%=path%>"/>
<%
i++;
}
}
%>
]
}
page / api.json.jsp (response.api.json.jsp包含的data []节点)
{
"uid": <%=getFormattedString("jcr:uuid", properties)%>,
"pageTitle": <sling:include path="pageTitle"/>, // text component
"body": <sling:include path="body"/> // parsys component
}
text / api.json.jsp (每页包含的pageTitle节点/ api.json.jsp)
{
"text": <%=getFormattedString("text", properties)%>,
"lastModified": <%=getFormattedDate("jcr:lastModified", properties)%>,
"resourceType": <%=getFormattedString("sling:resourceType", properties)%>
}
parsys / api.json.jsp (每个页面/ api.json.jsp包含的正文节点)
{
"resourceType": "${properties['sling:resourceType']}",
"children": [
<%
NodeIterator children = currentNode.getNodes();
int i = 0;
if (children != null && children.getSize() > 0) {
while (children.hasNext()) {
Node child = children.nextNode();
if (i > 0) {
%>
,
<%
}
%>
<sling:include path="<%=child.getPath()%>"/> // any component resource type
<%
i++;
}
}
%>
]
}
答案 0 :(得分:0)
不要使用sling:include来获取路径中的内容,这会引发一些递归选择器错误。您应该从路径中读取消息节点以获取json的适当内容值。这是一个例子。您可以替换&#34; / content / geometrixx-outdoors / en / women / jcr:content / par / banner&#34;与你的道路。
<%@ include file="/libs/foundation/global.jsp" %>
<%@ page session="false" import="org.apache.sling.api.resource.Resource" %>
<%@ page session="false" import="javax.jcr.Node" %>
<%
String yourMessage = "";
try {
Resource rs = resourceResolver.getResource("/content/geometrixx-outdoors/en/women/jcr:content/par/banner");
if (rs != null) {
Node node = rs.adaptTo(Node.class);
if (node.hasProperty("yourContentNode")) {
yourMessage = node.getProperty("yourContentNode").getString();
}
}
} catch (Exception e) {
}
%>
{
"metadata": {
"page": 1,
},
"data": [
<%= yourMessage %>,
{
"key1": "value 1",
"key2": "value2"
}
]
}