@Vijayalakshmi_S,
While using Sling Models, you can utilise the injector specific annotations.
Touch UI Dialogue:
<customTags
jcr:primaryType="nt:unstructured"
sling:resourceType="cq/gui/components/common/tagspicker"
fieldLabel="Custom Tags"
mode="contains"
name="./customTags"/>
Sling Model Java Class:
package com.myexamples.models;
import com.day.cq.tagging.Tag;
import com.day.cq.tagging.TagManager;
import lombok.Getter;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
@Model(adaptables = Resource.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class ExampleSlingModel {
@SlingObject
private ResourceResolver resourceResolver;
@Getter
@ValueMapValue
private String[] customTags;
@Getter
private List<Tag> tags = new ArrayList<>();
@PostConstruct
private void init() {
resolveTags();
}
private void resolveTags() {
if (customTags != null) {
TagManager tagManager = resourceResolver.adaptTo(TagManager.class);
for (int i = 0, n = customTags.length; i < n; i++) {
Tag tag = tagManager.resolve(customTags[i]);
if (tag != null) {
tags.add(tag);
}
}
}
}
}
Sightly:
<sly data-sly-use.example="com.myexamplels.model.ExampleSlingModel"></sly>
<ul data-sly-list.tag="${example.tags}">
<li>${tag.path}, ${tag.tagID}, ${tag.name}</li>
</ul>
Tip: The code above assumes that the current user has view access to the cq:Tags. You can use service-users to obtain the TagManager if permissions are not granted to specific users.
Never forget to write JUNIT Tests for your Java Sling Models: https://sourcedcode.com/aem-sling-models-unit-test-junit-4-with-examples
I hope this helps.