我已經創建了一個OSGI配置如下讀取值的servlet
@Property(label = "Social Media", value = "", unbounded = PropertyUnbounded.ARRAY, description = "Enter a social media string configuration")
它是一個數組屬性。我想將它讀入servlet以使其可以顯示在頁面上。請幫助我。
我已經創建了一個OSGI配置如下讀取值的servlet
@Property(label = "Social Media", value = "", unbounded = PropertyUnbounded.ARRAY, description = "Enter a social media string configuration")
它是一個數組屬性。我想將它讀入servlet以使其可以顯示在頁面上。請幫助我。
請看看這篇Adobe Helpx文章在: - https://helpx.adobe.com/experience-manager/using/osgi_config.html
//讀AEM OSGi的配置值
代碼: -
package org.testsite.core.service.impl;
import java.util.Map;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.PropertyUnbounded;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testsite.core.service.ConfigurationService;
@Service({ConfigurationServiceImpl.class})
@Component(immediate=true, metatype=true, label="Example Configuration Service")
public class ConfigurationServiceImpl
implements ConfigurationService
{
private static final Logger LOG = LoggerFactory.getLogger(ConfigurationServiceImpl.class);
private static final String CLASS_NAME = "[ConfigurationService]: ";
@Property(unbounded=PropertyUnbounded.ARRAY, label="Multi String", cardinality=50, description="Example for Multi field config")
private static final String MULTI_FIELD = "multifield";
@Property(unbounded=PropertyUnbounded.DEFAULT, label="Simple String", description="Example for Simple text field config")
private static final String SIMPLE_FIELD = "simplefield";
private String[] multiString;
private String simpleString;
public String[] getMultiString()
{
return this.multiString;
}
public String getSimpleString()
{
return this.simpleString;
}
@Activate
protected void activate(Map<String, Object> properties)
{
LOG.info("[*** AEM ConfigurationService]: activating configuration service");
readProperties(properties);
}
protected void readProperties(Map<String, Object> properties)
{
LOG.info(properties.toString());
this.multiString = PropertiesUtil.toStringArray(properties.get("multifield"));
LOG.info("Mutli String Size: " + this.multiString.length);
this.simpleString = PropertiesUtil.toString(properties.get("simplefield"), "default");
LOG.info("Simple String: " + this.simpleString);
}
}
我希望這會幫助你。
感謝和問候 Kautuk薩尼
一種方法是使用org.apache.sling.commons.osgi.PropertiesUtil#toStringArray
方法:
package com.foo.bar.service.impl;
import java.util.Map;
import org.apache.commons.lang.ArrayUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.PropertyUnbounded;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
@Service(value = SampleService.class)
@Component(metatype = true)
@Properties({
@Property(
label = "Sample Service",
description = "Sample service demonstrating an array setting",
name = SampleServiceImpl.MY_PROPERTY_NAME,
unbounded = PropertyUnbounded.ARRAY)
})
public class SampleServiceImpl implements SampleService {
public static final String MY_PROPERTY_NAME = "sample.myarray";
private String[] myArraySetting;
@Activate
protected void activate(Map<String, Object> properties) {
myArraySetting = PropertiesUtil.toStringArray(properties.get(MY_PROPERTY_NAME), ArrayUtils.EMPTY_STRING_ARRAY);
}
@Deactivate
protected void deactivate() {
}
}
感謝@nateyolles我正在一個錯誤ArrayUtils.EMPTY_STRING_ARRAY –
你有'org.apache.commons.lang.ArrayUtils'或'org.apache.commons.lang3。 ArrayUtils'在你的項目中?否則,你可以用'new String [] {}'替換它。 – nateyolles
謝謝@nateyolles它正在工作 –