Display Composite multifield on pages using sling model and htl | Community
Skip to main content
New Participant
August 16, 2022
Solved

Display Composite multifield on pages using sling model and htl

  • August 16, 2022
  • 2 replies
  • 1524 views

 
## Below is my sling Model##
 
package com.impeccables.core.core.models.Impl;


import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;

import org.apache.sling.models.annotations.Model;
//import org.apache.sling.models.annotations.Via;

//import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
//import org.apache.sling.models.annotations.injectorspecific.SlingObject;
//import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;

//import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import java.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//import com.day.cq.wcm.api.Page;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
//import javax.inject.Named;

import com.impeccables.core.core.models.TestMulti;

@Model(
        adaptables = SlingHttpServletRequest.class,
        adapters = TestMulti.class,
       
        defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)

public class TestMultiImpl implements TestMulti {
    private static final Logger LOGGER = LoggerFactory.getLogger(TestMultiImpl.class);
   

    // @ScriptVariable
    // Page currentpage;

    // @SlingObject
    // SlingHttpServletRequest slingHttpServletRequest;

 

    @Inject
    Resource componentResource;
 
 @Override
    public List<Map<String, String>> getCompositeField() {
        List<Map<String, String>> productDetailsMap=new ArrayList<>();
        try {
            Resource productDetail=componentResource.getChild("compositefield");
            if(productDetail!=null){
                for (Resource top : productDetail.getChildren()) {
                    Map<String,String> productMap=new HashMap<>();
                    productMap.put("product",top.getValueMap().get("product",String.class));
                    productMap.put("sold",top.getValueMap().get("sold",String.class));
                   
                    productDetailsMap.add(productMap);
                }
            }
        }catch (Exception e){
            LOGGER.info("\n ERROR while getting Product Details {} ",e.getMessage());
        }
        LOGGER.info("\n SIZE {} ",productDetailsMap.size());
        return productDetailsMap;
    }
 
 
#component Node structure#

 

 <field
       name=./compositefield
 field>
 
 
 
can you suggest some simple methods to display those item0 and item1 on page ?
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by arunpatidar

You can try @ChildResource injector

https://sling.apache.org/documentation/bundles/models.html 

 

 

@ChildResource
ChildrenList<ChildModel> compositefield;

 

2 replies

arunpatidar
arunpatidarAccepted solution
New Participant
August 17, 2022

You can try @ChildResource injector

https://sling.apache.org/documentation/bundles/models.html 

 

 

@ChildResource
ChildrenList<ChildModel> compositefield;

 

Arun Patidar
DEBAL_DAS
New Participant
August 17, 2022