在Felix注释中,Servlet代码如下: sling.servlet.resourceTypes的定义为unbounded = PropertyUnbounded.ARRAY
@Component(metatype = true, label = "Test Servlet", description = "Test Servlet", configurationFactory = true, policy = ConfigurationPolicy.REQUIRE)
@Service
@Properties({
@Property(name = "sling.servlet.resourceTypes", unbounded = PropertyUnbounded.ARRAY, label = "Sling Resource Type", description = "Sling Resource Type for the Home Page component or components."),
@Property(name = "sling.servlet.selectors", value = "test-selector", propertyPrivate = true),
@Property(name = "sling.servlet.extensions", value = "html", propertyPrivate = true),
@Property(name = "sling.servlet.methods", value = "GET", propertyPrivate = true) })
但是当我按照以下方式将其更改为OSGI注释时,我找不到在@Component注释内初始化此sling.servlet.resourceTypes的方法
@Designate(ocd = TestServlet.Config.class)
@Component(name = "com.myproject.TestServlet", service = Servlet.class, property = {
"service.description=Test Servlet", "sling.servlet.methods=GET",
"sling.servlet.resourceTypes=" + ??, "sling.servlet.extensions=" + "html",
"sling.servlet.selectors=" + "test-selector" }
)
public final class TestServlet extends SlingSafeMethodsServlet {
private String[] allowedResourceTypes;
@Activate
protected void activate(Config config) {
this.config = config;
allowedResourceTypes = config.getResourceTypes();
}
@ObjectClassDefinition(name="Test servlet config")
public @interface Config {
@AttributeDefinition(
name = "Resource Types",
description = "The set of resource types.",
type = AttributeType.STRING
)
String[] getResourceTypes() ;
}
}
更新:除了通过代码注册外,我找不到简单的解决方案。一个示例可能是core Adaptive component
答案 0 :(得分:0)
如果您想拥有多种资源类型(基本上是一种资源类型的数组),则必须分别命名每一个:
@Designate(ocd = TestServlet.Config.class)
@Component(
name = "com.myproject.TestServlet",
service = Servlet.class, property = {
"service.description=Test Servlet",
"sling.servlet.methods=GET",
"sling.servlet.resourceTypes=nt:file",
"sling.servlet.resourceTypes=project/components/mycomp",
"sling.servlet.extensions=html",
"sling.servlet.selectors=" + "test-selector"
}
)
public final class TestServlet extends SlingSafeMethodsServlet {
private String[] allowedResourceTypes;
@Activate
protected void activate(Config config) {
this.config = config;
allowedResourceTypes = config.getResourceTypes();
}
@ObjectClassDefinition(name="Test servlet config")
public @interface Config {
@AttributeDefinition(
name = "Resource Types",
description = "The set of resource types.",
type = AttributeType.STRING
)
String[] getResourceTypes() ;
}
}
所以诀窍是要有多个 sling.servlet.resourceTypes 条目。
希望有帮助。