Expand my Community achievements bar.

SOLVED

Sling Models + sightly to read multifield values

Avatar

Level 2

Hello Everyone,

I have a multifield which stores data in node structure. I am using sling models to read it. When i drag and drop the component,I get following error

"org.apache.sling.scripting.sightly.SightlyException: Identifier com.test.core.models.WCMUsejsDemo cannot be correctly instantiated by the Use API".

After page refresh there is no error.

Multifield is stored as shown below:

content_structure.PNG

Code of WCMUsejsDemo:

import javax.inject.Inject;

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

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

@Model(adaptables=Resource.class)

public class WCMUsejsDemo {

@Inject

public Resource userstory;

}

Thanks,

Megha Gowroji

1 Accepted Solution

Avatar

Correct answer by
Level 10

Best way in AEM 6.3 to use Multifield is to use the Granite/Coral MF type and Sling Models- as discussed here:

Adobe Experience Manager Help | Creating a HTL Repeating Data Set 6.3 Component that uses Sling Mode...

Notice the use of Sling Models and HLT to read the MF in this example.

Then you can easily render out the MF values to the web page - such as:

client.png

View solution in original post

5 Replies

Avatar

Level 3

You should try using the @ChildResource annotation instead of @Inject

Avatar

Community Advisor

Yes using @ChildResource you can get multifield value

@ChildResource(injectionStrategy = InjectionStrategy.OPTIONAL)

private Resource multinodes;

Once you get the childResource, iterate through all the child nodes

Iterator<Resource> iterator = multinodes.listChildren();

Avatar

Correct answer by
Level 10

Best way in AEM 6.3 to use Multifield is to use the Granite/Coral MF type and Sling Models- as discussed here:

Adobe Experience Manager Help | Creating a HTL Repeating Data Set 6.3 Component that uses Sling Mode...

Notice the use of Sling Models and HLT to read the MF in this example.

Then you can easily render out the MF values to the web page - such as:

client.png

Avatar

Level 2

Thank you Scott.

Adding @Optional annotation worked for me.