Page Model Json having issue to retrieve child nodes(Multifield items) | Community
Skip to main content
Level 2
May 1, 2018

Page Model Json having issue to retrieve child nodes(Multifield items)

  • May 1, 2018
  • 3 replies
  • 12091 views

When I am trying to access the page model json, I see just the sling resource type component name and not the exact node values. I tried different ways and when I say <page-name>.model.json, it always returns the nodes that are at first level. Child nodes are not being retrieved. To explain it better see the following JSON response.

"productlist": {

"columnClassNames": "aem-GridColumn aem-GridColumn--default--12",

":type": "demo/components/content/productlist",

"headlineText": "Product List"

}

if you see the above JSON which is rendered when I did page mode json and it has a component called ProductList which is a multifield type. IT returns the type of component and not exact values inside it. I tried all ways using Jackson annotations to make it work, but no luck. So if anyone has faced the same issue and could help me with the best approach, it would be helpful.

When I checked List component, it has one multifield value as text and it is stored as String[] in JCR. I am not sure how can I do that with the composite multifield. Because under productlist node I will be having node structure as below.

Each node having two properties. It could be more. To retrieve the values, I have a sling model with these two properties and I am referencing the same model as List<LinksModel> links where LinksModel is the sling model with above mentioned two properties.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

3 replies

Hemant_arora
Level 8
May 2, 2018

Does .infinity.json solve your purpose?

http://localhost:4504/content/AEM63App/en.infinity.json

Level 2
May 2, 2018

@Hemant,

then what is the purpose of having Sling Models and exporter framework ? I want to use sling models to retrieve specific fields using sling models.

smacdonald2008
Level 10
May 2, 2018

"I want to use sling models to retrieve specific fields using sling models."

To render MF values onto a web page by Sling Models (which is the main use case for using Multi-fields - not to render as JSON)  - its straightforward.

See this Article -- Scott's Digital Community: Creating a HTL Repeating Data Set 6.3 Component that uses Sling Models

There is very little Java code. You Inject the node that represents the Multi field - as shown in the article. The Java code:

package com.htl.community.coral.core.models;

import javax.annotation.PostConstruct;

import javax.inject.Inject;

import javax.inject.Named;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.models.annotations.Default;

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

import org.apache.sling.models.annotations.Optional;

import org.apache.sling.settings.SlingSettingsService;

@Model(adaptables = Resource.class)

public class Multifield {

    // Inject the products node under the current node

    @Inject

    @Optional

    public Resource products;

    // No need of a post construct as we don't have anything to modify after the

    // model is constructed

}

Then you can write out the MF values in HTL like this:

<div

    data-sly-use.multiItems="com.htl.community.coral.core.models.Multifield">

    <div data-sly-list.head="${multiItems.products.listChildren}">

        <div style="height:250px;"><img src=${head.pathbr} height=200 width=270 style="padding:4px"/><h2>${head.product}</h2>

                             <p>${head.desc}</p>

                                

                             </div>

                             <hr>

   </div>

</div>

When you render MF values as HTML - the output is shown in the web page. For example:

VeenaVikraman
Community Advisor
Community Advisor
May 2, 2018

Hi Narendran

    Could you please explain me what is the exact use case you are trying to acheive? I believe you are trying to fetch the values of multiflield using Sling models ? I couln't understand your question exactly

Thanks

Veena

Level 2
May 2, 2018

Hi Veena,

Go to any page which is ootb, and then hit model.json. For ex: http://localhost:4502/content/we-retail/us/en.model.json.

If you check this Page Json, you see all the components that are being used on that page which is expected. Now create any composite multifield component and drop it on the page as I did in my original post. It is not needed to be complex. Just have a multifield with two fields, 1) Text and 2) path field.

Now drop the same component on the same en.html page and try to render the model.json. Now you will see the component type and not the actual content there. I see Model json is returning the first level component values and not the child nodes under those components. Let me know if you need more details.

September 13, 2019

Hi narendran63116907​ / iainc65537558

Below steps should resolve your issue
  1. Add adapters annotation to your model class like adapters = { ComponentExporter.class }
  2. Make your sling model Implement ComponentExporter interface and override getExportedType() method. This method should return the sling:resourceType path for your component
July 15, 2020

Hi Narendran,

 

I am facing a similar issue like this. Did you find a solution for this? 

 

Thanks,

Rajeswari K