Dynamic injection of values into sling model is not working
Hi All,
i am trying to inject some values dynamically into sling model and retrieve some properties of that injected value and print them as below which is not working and throwing a null pointer exception which is not appropriate.
i can see sling model is registered in adapters.
some one please tell me where it went wrong.
Any help is highly appreciated.
In below i want to send each item of list dynamically to sling model via "selectPath" and process the valur for a property in sling model and retrieve in "flags.flagicon"
my sightly html:
<select class="list-selector__dropdown language-change" title="languages" data-sly-list.item="${listNavigation.items}">
<sly data-sly-use.flags="${'com.krish.chopps.modules.sling.models.listselector.ListSelectorModel' @ selectPath='${item.path}' }">
<option value="${item.language}">${item.title}, ${flags.flagicon}</option>
</select>
</sly>
My Sling model:
@Model(adaptables = { SlingHttpServletRequest.class, Resource.class })
public class ListSelectorModel {
private static final Logger logger = LoggerFactory.getLogger(ListSelectorModel.class);
@RequestAttribute
@Optional
private String selectPath;
@SlingObject
private ResourceResolver resolver;
public String getFlagicon() {
return flagicon;
}
private String flagicon;
@PostConstruct
public void init() {
if (selectPath != null) {
flagicon = getNodeValue(selectPath, "countryFlag");
}
}
private String getNodeValue(String parentPage, String property) {
logger.info("parentpage is "+ parentPage);
Resource resource = resolver.getResource(parentPage + "/jcr:content");
Node node = resource.adaptTo(Node.class);
String image = null;
try {
if (node != null && node.hasProperty(property)) {
image = node.getProperty(property).getString();
}
} catch (RepositoryException e) {
logger.info("Exception in node values : {}", e);
}
return image;
}
}