AEM 6.3:调用POST Servlet时出错

时间:2017-11-21 03:30:17

标签: servlets aem sling

我创建了一个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和selector调用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 debuuger上看到的错误。在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映射的结果

enter image description here

2 个答案:

答案 0 :(得分:1)

由于某种原因,调用POST默认POST servlet时。自ModifyOperation执行以来,这一点清晰可见。

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

答案 1 :(得分:1)

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

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

也不建议对路径进行硬编码。我过去为这种情况做的是拥有一个组件,在你的情况下,你可以称之为订阅组件,表格可以像 -

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

并且您的servlet resourceType是组件。