【问题标题】:CQ5 multifield configuration serviceCQ5多字段配置服务
【发布时间】:2016-07-28 14:21:44
【问题描述】:

我正在尝试使用多字段配置界面创建 CQ5 服务。它类似于this,但在单击加号按钮时,它不仅会添加新行,还会添加一组 N 行。

房产

  • 字段1 +-
  • 字段2
  • ....
  • 字段N

有什么建议吗?

【问题讨论】:

    标签: aem


    【解决方案1】:

    据我所知,Apache Felix 中没有这种可能性。

    根据您的实际要求,我会考虑分解配置。尝试将所有字段集(您希望通过加号按钮添加的字段组)移动到单独的配置中。因此,与slf4j.Logger 配置非常接近,您将拥有一个配置工厂方法。

    一个简单的配置工厂如下所示

    @Component(immediate = true, configurationFactory = true, metatype = true, policy = ConfigurationPolicy.OPTIONAL, name = "com.foo.bar.MyConfigurationProvider", label = "Multiple Configuration Provider")
    @Service(serviceFactory = false, value = { MyConfigurationProvider.class })
    @Properties({
            @Property(name = "propertyA", label = "Value for property A"),
            @Property(name = "propertyB", label = "Value for property B") })
    public class MyConfigurationProvider {
    
        private String propertyA;
        private String propertyB;
    
        @Activate
        protected void activate(final Map<String, Object> properties, final ComponentContext componentContext) {
            propertyA = PropertiesUtil.toStringArray(properties.get("propertyA"), defaultValue);
            propertyB = PropertiesUtil.toStringArray(properties.get("propertyB"), defaultValue);
        }
    }
    

    使用它就像在任何@Component中添加引用一样简单

    @Reference(cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE, referenceInterface = MyConfigurationProvider.class, policy = ReferencePolicy.DYNAMIC)
    private final List<MyConfigurationProvider> providers = new LinkedList<MyConfigurationProvider>();
    
    protected void bindProviders(MyConfigurationProvider provider) {
        providers.add(provider);
    }
    
    protected void unbindProviders(MyConfigurationProvider provider) {
        providers.remove(provider);
    }
    

    【讨论】:

      【解决方案2】:

      这是一种方法。

      @Component(label = "My Service", metatype = true, immediate = true)
      @Service(MyService.class)
      @Properties({
          @Property(name = "my.property", description = "Provide details Eg: url=http://www.google.com|size=10|path=/content/project", value = "", unbounded = PropertyUnbounded.ARRAY) })
      
      public class MyService {
      
      private String[] myPropertyDetails;
      
      @Activate
      protected void activate(ComponentContext ctx) {
          this.myPropertyDetails = getPropertyAsArray(ctx.getProperties().get("my.property"));
          try {
              if (null != myPropertyDetails && myPropertyDetails.length > 0) {
                  for(String myPropertyDetail : myPropertyDetails) {
                      Map<String, String> map = new HashMap<String, String>();
                      String[] propertyDetails = myPropertyDetails.split("|");
                      for (String keyValuePair : propertyDetails) {
                          String[] keyValue = keyValuePair.split("=");
                          if (null != keyValue && keyValue.length > 1) {
                              map.put(keyValue[0], keyValue[1]);
                          }
                      }                   
                      /* the map now has all the properties in the form of key value pairs for single field 
                          use this for logic execution. when there are no multiple properties in the row, 
                          you can skip the  logic to split and add in the map */
                  }
              }
          } catch (Exception e) {
              log.error( "Exception ", e.getMessage());
          }
      }
      
      private String[] getPropertyAsArray(Object obj) {
          String[] paths = { "" };
          if (obj != null) {
              if (obj instanceof String[]) {
                  paths = (String[]) obj;
              } else {
                  paths = new String[1];
                  paths[0] = (String) obj;
              }
          }
          return paths;
      }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-24
        • 1970-01-01
        • 2021-09-20
        • 1970-01-01
        • 2013-12-05
        • 1970-01-01
        相关资源
        最近更新 更多