gpt4 book ai didi

javascript - AEM 6.3 : Sling Servlet registered with resourceType

转载 作者:行者123 更新时间:2023-12-02 12:06:23 28 4
gpt4 key购买 nike

我创建了一个使用 AJAX 调用的 Sling servlet。这是 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.SlingSafeMethodsServlet;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Modified;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.metatype.annotations.Designate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aem.sites.interfaces.SearchConfiguration;


@Component(
immediate = true,
service = Servlet.class,
configurationPid = "com.aem.sites.servlets.SearchServlet",
property = {
"sling.servlet.methods=get",
"sling.servlet.selectors=search",
"sling.servlet.resourceTypes=aemsite-project/components/structure/page",
"sling.servlet.extensions=html"
}
)
@Designate(ocd=SearchConfiguration.class)
public class SearchServlet extends SlingSafeMethodsServlet {

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

/** The Constant logger. */
private final Logger logger = LoggerFactory.getLogger(this.getClass());

@Override
protected void doGet(final SlingHttpServletRequest req,
final SlingHttpServletResponse resp) throws ServletException, IOException {
logger.info("********************************inside search servlet:::do get*****************************************");
}

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

}


package com.aem.sites.interfaces;

import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;

@ObjectClassDefinition(name="Search Configuration")
public @interface SearchConfiguration {

@AttributeDefinition(
name = "Enable",
description = "Sample boolean property"
)
boolean enabled() default false;

}

enter image description here

这是js文件:

$('#search-input input').keydown(function(event) {
var keyCode = (event.keyCode ? event.keyCode :event.which);
if(keyCode == 13) {
console.log('inside key event');
var searchterm = $("#search-input input").val();
console.log('value of search term is '+searchterm);
//$("searchForm").submit();
$.ajax({
type: "GET",
url: "/content/aemsite/en/results.search.html?q="+searchterm,
success: function(data) {
}
});
}

});

由于某种原因,servlet 没有被调用。我遵循了以下示例:

https://github.com/nateyolles/aem-osgi-annotation-demo/blob/master/core/src/main/java/com/nateyolles/aem/osgiannotationdemo/core/servlets/SampleOsgiServlet.java

http://www.aemcq5tutorials.com/tutorials/sling-servlet-in-aem/

我不确定我在这里缺少什么。

最佳答案

这会导致 DS 的“sling.servlet.methods”值区分大小写。您可以通过从“sling.servlet.methods=get”更新为“sling.servlet.methods=GET”来解决该问题。

package com.aem63.features.core.components.search;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Modified;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;

/**
* Ref: https://stackoverflow.com/questions/46881339/aem-6-3-sling-servlet-registered-with-resourcetype
*/

@Component(service = Servlet.class,
immediate = true,
configurationPid = "com.aem63.features.core.components.search.SearchServlet",
property = {
Constants.SERVICE_DESCRIPTION + "=Simple Demo Servlet",
"sling.servlet.methods=" + HttpConstants.METHOD_GET,
"sling.servlet.resourceTypes=" + "sling/servlet/default",
"sling.servlet.selectors=" + "search",
"sling.servlet.extensions=" + "data"

})
public class SearchServlet extends SlingSafeMethodsServlet {

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

/**
* The Constant logger.
*/
private final Logger logger = LoggerFactory.getLogger(this.getClass());

@Override
protected void doGet(final SlingHttpServletRequest req,
final SlingHttpServletResponse resp) throws ServletException, IOException {
logger.info("********************************inside search servlet:::do get*****************************************");
}

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

}

演示页面:http://localhost:4502/content/aem63-features/en/jcr:content/.en.txt

关于javascript - AEM 6.3 : Sling Servlet registered with resourceType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46881339/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com