Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Why can not inject value from cq dialog in sling model

Avatar

Level 2

I am getting trouble with sling model in AEM 6.3. The field value productData must be injected as expected with value whenever we change the value of pathbrowser in the component's dialog but It just gets null. I did try some solutions from the internet but it does not work.

Here is some referenced link I followed:

https://techrevel.blog/tag/sling-model/

Adobe CQ/Adobe AEM: How to use Sling Models in CQ5.6

My model code

import com.day.cq.wcm.api.Page;

import com.adobe.cq.commerce.api.Product;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

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

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

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

import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;

import org.apache.sling.models.annotations.injectorspecific.SlingObject;

import javax.annotation.PostConstruct;

import javax.inject.Inject;

import javax.inject.Named;

@Model(adaptables = {SlingHttpServletRequest.class, Resource.class})

public class ProductDetailsModel {

   @SlingObject
   private SlingHttpServletRequest request;

   @SlingObject
   private SlingHttpServletResponse response;

   @ScriptVariable
   private Page currentPage;

   @Inject
  @Named("productData")

   @Optional
   private String productData;

   @PostConstruct
   public void init(){

   int i = 1 + 2;

  Product product = request.getResourceResolver().getResource(productData).adaptTo(Product.class);

  }

   public String getProductData() {

   return productData;

  }

   public void setProductData(String productData) {

   this.productData = productData;

  }

}

My pathbrowser in dialog content:

<column
   jcr:primaryType="nt:unstructured"
   sling:resourceType="granite/ui/components/coral/foundation/container">

   <items jcr:primaryType="nt:unstructured">

   <productData
   jcr:primaryType="nt:unstructured"
   sling:resourceType="granite/ui/components/foundation/form/pathbrowser"
   fieldDescription="Display in page"
   fieldLabel="Path"
   name="./productData"/>

   </items>

</column>

I really do not know what I did wrong here. Please help me resolve this.

1 Accepted Solution

Avatar

Correct answer by
Level 10

(posted after someone clicked Assumed Anwered)

Here is an article that shows you how to work with Sling Models and Dialogs.

Building Experience Manager Components using Granite/Coral Resource Types

You should have HTL code that displays the value of productData. See the article above as an example - in the Sling Model section.

View solution in original post

3 Replies

Avatar

Correct answer by
Level 10

(posted after someone clicked Assumed Anwered)

Here is an article that shows you how to work with Sling Models and Dialogs.

Building Experience Manager Components using Granite/Coral Resource Types

You should have HTL code that displays the value of productData. See the article above as an example - in the Sling Model section.

Avatar

Level 2

Hi, I finally get the productData injected from dialog by trying this code

@Model(adaptables = {Resource.class}, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)

public class ProductDetailsModel {

private static final Logger LOGGER = LoggerFactory.getLogger(ProductDetailsModel.class);

@SlingObject

Resource resource;

@SlingObject

private ResourceResolver resourceResolver;

@Inject

private String productData;

@PostConstruct

public void init(){

if (productData != null) {

product = resourceResolver.getResource(productData).adaptTo(Product.class);

...

}

}

...

}

I now can also get resource and the resource Resolver. It did work to assign value to productData but this solution seems imposible to inject the sling servlet request and response. }