I am converting WCMusePojos to sling models and have run across an issue that I have been unable to resolve. For some reason the parameters from my HTL isn't being injected on the publish instance. This works fine on the author instance, but both variables are null on publish when I check the logs. What could be causing this issue?
Here's the model:
package com.my.aem.dam.core.portal;
import com.day.cq.wcm.api.components.ComponentContext;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.Model;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
/**
* Helps setting the component context with
* the product id passed as parameter from the HTL file
* so it can be used by other components on the page
* */
@Model(
adaptables = {SlingHttpServletRequest.class}
)
public class IdHelper {
@inject
private String productId;
@inject
ComponentContext context;
private static final String PRODUCT_ID = "productId";
private Logger logger = LoggerFactory.getLogger(IdHelper.class);
/**
* Obtains the product id as a parameter of the HTL and then sets it as
* attribute of the component context.
* */
@PostConstruct
public void activate() {
logger.info("productId is {}", productId);
logger.info("CompontentContext is {}", context);
context.setAttribute(PRODUCT_ID, productId);
}
}
And the HTL:
<div data-sly-use.accordion="com.adobe.cq.wcm.core.components.models.Accordion"
data-sly-use.modelCache="com.adobe.aem.commons.assetshare.util.ModelCache"
data-sly-test.asset="${modelCache['com.adobe.aem.commons.assetshare.content.AssetModel']}"
data-sly-use.productAccordion="${'com.my.aem.dam.core.portal.ProductAccordion' @ path=asset.path, addRepeatingNames=true}"
class="cmp-accordion"
data-cmp-is="accordion"
data-cmp-single-expansion="${accordion.singleExpansion}">
<div data-sly-test="${productAccordion.products.size > 0}"
data-sly-repeat.item="${productAccordion.products}"
class="cmp-accordion__item"
data-cmp-hook-accordion="item"
data-cmp-expanded="${item.name in productAccordion.products}">
<h3 data-sly-element="${item.name}"
class="cmp-accordion__header">
<button class="cmp-accordion__button${item.name in accordion.expandedItems ? ' cmp-accordion__button--expanded' : ''}"
data-cmp-hook-accordion="button">
<span class="cmp-accordion__title">${item.id}</span>
<span class="cmp-accordion__icon"></span>
</button>
</h3>
<div data-sly-use.productHelper="${'com.my.aem.dam.core.portal.IdHelper' @ productId=item.id}"
data-sly-resource="/content/my-portal/home/details/product-accordion-container/jcr:content/root/responsivegrid"
data-cmp-hook-accordion="panel"
class="cmp-accordion__panel${item.name in accordion.expandedItems ? ' cmp-accordion__panel--expanded' : ' cmp-accordion__panel--hidden'}"
role="region"></div>
</div>
<sly data-sly-resource="${resource.path @ resourceType='wcm/foundation/components/parsys/newpar', appendPath='/*', decorationTagName='div', cssClassName='new section aem-Grid-newComponent'}"
data-sly-test="${(wcmmode.edit || wcmmode.preview) && accordion.items.size < 1}"></sly>
</div>
Solved! Go to Solution.
Views
Replies
Total Likes
Hi,
Please check this if helps
https://github.com/arunpatidar02/aem63app-repo/blob/master/java/ParamModel.java
Views
Replies
Total Likes
could you please try @valuemapValue instead of @inject annotation
Views
Replies
Total Likes
I tried and they were still null on publish, but now also null on author.
Views
Replies
Total Likes
Dear @DNest19
Please check Sling Models documentation Apache Sling :: Sling Models
and for implementation
1. http://sgaem.blogspot.com/2017/08/deep-dive-on-sling-models-part-1.html
2. https://www.techinnovia.com/sling-model/
Hope that helps you!
Regards,
Santosh
Views
Replies
Total Likes
Hi,
Please check this if helps
https://github.com/arunpatidar02/aem63app-repo/blob/master/java/ParamModel.java
Views
Replies
Total Likes
I was able to populate the portalId based on your suggestion, but the componentcontext is still null. Do you have any suggestions for that particular issue? Thank you for your help!
package com.my.aem.dam.core.portal; import com.day.cq.wcm.api.components.ComponentContext; 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.injectorspecific.RequestAttribute; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.annotation.PostConstruct; import javax.inject.Inject; /** * Helps setting the component context with * the product id passed as parameter from the HTL file * so it can be used by other components on the page * */ @Model( adaptables = {SlingHttpServletRequest.class, Resource.class}, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL ) public class IdHelper { @RequestAttribute(name="productId") private String productId; @Inject ComponentContext context; private static final String PRODUCT_ID = "productId"; private Logger logger = LoggerFactory.getLogger(IdHelper.class); /** * Obtains the product id as a parameter of the HTL and then sets it as * attribute of the component context. * */ @PostConstruct public void activate() { logger.info("productId is {}", productId); logger.info("CompontentContext is {}", context); context.setAttribute(PRODUCT_ID, productId); } }
Views
Replies
Total Likes
After some tinkering I have managed to get it to work based on your resource. Thank you so much!
Here's how I injected the context:
@ScriptVariable(name="componentContext") ComponentContext context;
Views
Replies
Total Likes
Views
Likes
Replies