【问题标题】:AEM 6.3: Error while calling POST ServletAEM 6.3:调用 POST Servlet 时出错
【发布时间】:2018-05-04 10:07:14
【问题描述】:

我创建了一个如下所示的 POST servlet:

package com.aem.sites.servlets;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Modified;
import org.osgi.service.metatype.annotations.Designate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aem.sites.interfaces.SubscriptionConfiguration;

@Component(immediate=true,
service=Servlet.class,
configurationPid="com.aem.sites.servlets.SubscriptionServlet",
property = {
        "sling.servlet.methods=POST", 
        "sling.servlet.selectors=newsletters",
        "sling.servlet.resourceTypes=aemsite-project/components/structure/page",
        "sling.servlet.extensions=html" 
}
        )
@Designate(ocd=SubscriptionConfiguration.class)
public class SubscriptionServlet extends SlingAllMethodsServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;


    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    protected void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws ServletException, IOException  {
        logger.info("====================================================SubscriptionServlet::::::::::SubscriptionConfiguration=====================================");
    }

    @Activate
    @Modified
    protected void Activate(SubscriptionConfiguration config) {
        logger.info("********************************inside SubscriptionConfiguration servlet*****************************************");
    }

}

我正在使用 resourceType 和选择器调用 servlet。正在通过表单调用 servlet:

<div id = "sign-up" style ="padding-top:6%;padding-left:2%">
  <div>
    <h1 style="font-size:2em">Subscribe to Newsletters</h1>
    <form name = "subscriptionForm" method = "POST" action="/content/aemsite/en/subscribe.newsletters.html" id="subscriptionForm">
    <input type="text" name="name" id="name" placeholder="Name" style="margin-bottom:1%;width:30%"/>
    <input type="text" name="email" id="email" placeholder="E-mail" style="margin-bottom:1%;width:30%"/>

    <input type="submit" name="signup_submit" value="Sign me up" style="margin-bottom:3%"/>
    </form>
  </div> 

</div>

这是作为 clientlibs 包含的 javascript 文件

$(function() {
    $('#subscriptionForm').submit(function(e) {
        e.preventDefault(); //STOP default action
        var formURL = $(this).attr("action");
        var method = $(this).attr("method");
        var name = $('#name').val();
        var email = $('#email').val();
        var form_data = $(this).serialize(); 
        console.log('inside subscription form '+form_data+' formURL '+formURL);
        $.ajax({
            type:method,
            url:formURL,
            data: form_data, success:function(data){
              }
        });
    });
});

我正在使用 AEM 开箱即用的 jQuery。当我提交表单时,它会引发一些错误。这些是我看到的错误:

Failed to load resource: the server responded with a status of 500 (Server Error)

以上是我在 chrome 调试器上看到的错误。在 error.log 文件中,我看到了这个错误:

20.11.2017 22:13:42.802 *ERROR* [0:0:0:0:0:0:0:1 [1511234022796] POST /content/aemsite/en/subscribe.newsletters.html HTTP/1.1] org.apache.sling.servlets.post.impl.operations.ModifyOperation Exception during response processing.
javax.jcr.nodetype.ConstraintViolationException: No matching property definition: name = aemsite

POST /content/aemsite/en/subscribe.newsletters.html HTTP/1.1] org.apache.sling.jcr.resource.internal.helper.JcrPropertyMapCacheEntry converToType: Cannot convert value of 2017/11/18 02:28:18 to class java.util.Calendar
java.lang.IllegalArgumentException: Not a date string: 2017/11/18 02:28:18

看起来 POST 请求正在尝试保存属性,但在 servlet 中,我要做的只是输出 servlet 的 doPost 方法中设置的记录器消息。当我将请求类型从 POST 更改为 GET 时,错误消失并且 servlet 被调用。

我了解到,从 AEM 6 开始,对 POST 请求采用了更多安全措施,因此使用了 CSRF 令牌,但如果使用 AEM 的 jQuery 版本,则主要处理它。

我不确定我做错了什么。任何帮助表示赞赏。

提前致谢

更新:

jcrresolver 映射结果

【问题讨论】:

  • 您是否检查过 URL /content/aemsite/en/subscribe.newsletters.html 是否正确解析为您的 servlet?您可以通过资源解析器控制台/system/console/jcrresolver 进行验证。还要验证您的 servlet 是否处于活动状态。
  • 感谢您的回复。我可以确认 servlet 处于活动状态,并且我附上了 jcrresolver 的屏幕截图。

标签: servlets aem sling


【解决方案1】:

当您发布到解析为 cq:Page 的路径时,您的具有 resourceType 的 servlet 将不会被调用。

发布到 - your_page_path/jcr:content 以获取基于资源的 servlet 来处理此请求

也不建议对路径进行硬编码。我过去在这种情况下所做的是拥有一个组件,在您的情况下,您可以将其称为订阅组件,并且可以像这样发布表单 -

 <form action="${resource.path}.yourselector.html" method="post">

而你的 servlet resourceType 就是组件。

【讨论】:

  • 这应该是公认的答案。您不仅提到了根本原因,还提到了解决方案。欣赏它。
【解决方案2】:

POSTing 调用默认的POST servlet 时,出于某种原因。自从ModifyOperation 被执行后,这一点清晰可见。

我建议您使用Recent Requests console page 来查看您的资源是如何识别的以及脚本是如何解析的。

【讨论】:

    【解决方案3】:

    我最近发现尝试使用 resourceType 和 OSGi 进行 POST 请求实际上可能不起作用。当我使用路径调用 POST 时,一切正常。我使用过 OSGi DS 和使用 resourceType 的 GET 调用,它运行良好,但将其更改为 POST 并没有真正起作用。我不知道这是否是一个错误,但这是我现在可以得出的结论。使用路径是进行 POST 调用的方式。

    【讨论】:

      猜你喜欢
      • 2017-01-05
      • 2018-04-18
      • 1970-01-01
      • 1970-01-01
      • 2018-07-10
      • 1970-01-01
      • 1970-01-01
      • 2014-05-13
      • 1970-01-01
      相关资源
      最近更新 更多