Aem CQ5组件后编辑

Aem CQ5组件后编辑,aem,sling,Aem,Sling,我创建了一个这样的骨架 @SlingServlet(paths = "/bin/foo/bar", methods = "POST") public class FooBarServlet extends SlingAllMethodsServlet { private final Logger LOGGER = LoggerFactory.getLogger(this.getClass()); @Override protected void doPost(Sling

我创建了一个这样的骨架

@SlingServlet(paths = "/bin/foo/bar", methods = "POST")
public class FooBarServlet extends SlingAllMethodsServlet {

    private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
    @Override
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {

        response.setHeader("Content-Type", "text/plain");
        response.getWriter().write("foo bar");
        LOGGER.info("hello world");
    }
}
我为我的组件创建了一个编辑配置

<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0"
    xmlns:jcr="http://www.jcp.org/jcr/1.0"
    jcr:primaryType="cq:EditConfig">
    <cq:listeners jcr:primaryType="cq:EditListenersConfig"
                  afteredit="myapp.components.foobar" />
</jcr:root>
我的页面加载,我的组件加载,我的clientlib js加载,我在控制台中没有看到任何错误。我编辑我的组件并点击ok。我的servlet被命中,我跟踪日志服务器端,没有看到任何错误。当我打开控制台进行跟踪时,在客户端看不到任何错误。我的回答是200行。一切看起来都很棒!除了在浏览器的右上角不断出现“未指定的错误”

如果我在服务器端日志上没有看到错误,在客户端控制台上也没有看到错误,那么有人知道我从哪里开始解决这个问题吗

更新

感谢@rakhi4110的参考。我从那个文档中找到了一些东西

首先,设置
suppressErrorMsg
标志隐藏错误消息

CQ.HTTP.post('/bin/foo/bar', oncomplete, { path : component.path }, null, true);
第二,我不喜欢压制事情,所以我试着像这样精心设计我的反应

{
    "headers" :
    {
         "Status":200,
         "Message":"foo bar"
    }
}
然而,这没有起到任何作用

第三个,在查看CQ.httpapi时,我注意到它的很多内容都被抹黑了,取而代之的是。简单地使用post函数,不使用suppress,就可以工作了

CQ.shared.HTTP.post('/bin/foo/bar', oncomplete, { path : component.path });

目前,我坚持使用选项3,直到我能够找到正确的json响应。

未指定的错误是由于应用于
CQ.HTTP.post()的默认配置造成的。

它尝试从响应头中检索消息并通知用户。由于您的自定义servlet不提供任何此类消息,因此您收到了此通知

通过将
suppressErrorMsg
参数设置为true,可以抑制此通知。i、 e

CQ.HTTP.post('/bin/boo/bar', oncomplete, { path : component.path }, null, true);
进一步观察,通知消息似乎是在以下情况下根据响应生成的:

  • 响应是HTML
  • 响应包含一个id为“Message”的HTML标记。在这种情况下,标记的内容被视为消息
  • 通知消息可能使用的示例HTML响应如下

    <html>
    <head>
        <title>OK</title>
    </head>
    <body>
    <h1>OK</h1>
    <table>
        <tbody>
            <tr>
                <td>Status</td>
                <td><div id="Status">200</div></td>
            </tr>
            <tr>
                <td>Message</td>
                <td><div id="Message">Demo Notification Message</div></td>
            </tr>
        </tbody>
    </table>
    </body>
    </html>
    
    
    好啊
    好啊
    地位
    200
    消息
    演示通知消息
    
    有关进一步配置,请参阅

    在servlet中

    @Override
        protected final void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response)
            throws ServletException, IOException {
            final String value = request.getRequestParameter("value").getString();
                // create response
    
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
    
            try {
                final JsonGenerator generator = FACTORY.createJsonGenerator(response.getWriter());
    
                MAPPER.writeValue(generator, Collections.singletonMap("Message", "success"));
            } catch (final JsonGenerationException jge) {
                LOG.error("error generating JSON response", jge);
            } catch (final JsonMappingException jme) {
                LOG.error("error mapping JSON response", jme);
            } catch (final IOException ioe) {
                LOG.error("error writing JSON response", ioe);
            }
        }
    
    在对话框中,执行以下操作

    postfunction="function(value) {
        var dialog = this.findParentByType('dialog');
        var url = CQ.HTTP.addParameter(dialog.path + '.json', 'value', value);
        var result = CQ.HTTP.eval(url);
        return result;
    }
    

    您可以使用其中一个ootb sling类来生成正确的响应:

    HtmlResponse-->用于HTML格式的响应

    JSONResponse-->用于JSON格式的响应

    all继承自,您可以使用其中一个onXXX方法来编写更改

    例如:

    JSONResponse result = new JSONResponse();
    result.setStatus(200, "Content changed");
    result.onChange;
    result.onCopied;
    result.onCreated;
    result.onDeleted;
    result.onModified;
    result.onMoved;
    boolean setStatus = true;
    
    result.send(slingHttpServletResponse, setStatus);
    

    镇压奏效了。我试图创建一个类似{“headers”:{“Status”:200,“Message”:“foobar”}的json响应。关于正确的json响应有什么建议吗?你能回答这个问题吗?我不同意。如果链接页面更改,则表示答案不再正确/有效。在这种情况下,我将避免复制和粘贴。我不会把这当作一般规则。
    postfunction="function(value) {
        var dialog = this.findParentByType('dialog');
        var url = CQ.HTTP.addParameter(dialog.path + '.json', 'value', value);
        var result = CQ.HTTP.eval(url);
        return result;
    }
    
    JSONResponse result = new JSONResponse();
    result.setStatus(200, "Content changed");
    result.onChange;
    result.onCopied;
    result.onCreated;
    result.onDeleted;
    result.onModified;
    result.onMoved;
    boolean setStatus = true;
    
    result.send(slingHttpServletResponse, setStatus);