2016-06-07 56 views
0

我已經創建了一個OSGI配置如下讀取值的servlet

@Property(label = "Social Media", value = "", unbounded = PropertyUnbounded.ARRAY, description = "Enter a social media string configuration") 

它是一個數組屬性。我想將它讀入servlet以使其可以顯示在頁面上。請幫助我。

回答

0

請看看這篇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); 
    } 
} 

另一篇文章涉及相同的問題: - http://help-forums.adobe.com/content/adobeforums/en/experience-manager-forum/adobe-experience-manager.topic.html/forum__pg8f-i_am_extendingmymo.html

我希望這會幫助你。

感謝和問候 Kautuk薩尼

1

一種方法是使用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() { 
    } 
} 
+0

感謝@nateyolles我正在一個錯誤ArrayUtils.EMPTY_STRING_ARRAY –

+0

你有'org.apache.commons.lang.ArrayUtils'或'org.apache.commons.lang3。 ArrayUtils'在你的項目中?否則,你可以用'new String [] {}'替換它。 – nateyolles

+0

謝謝@nateyolles它正在工作 –