Why can not inject value from cq dialog in sling model | Community
Skip to main content
dangl95641558
Level 2
May 4, 2018
Solved

Why can not inject value from cq dialog in sling model

  • May 4, 2018
  • 3 replies
  • 3045 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by smacdonald2008

(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.

3 replies

smacdonald2008
smacdonald2008Accepted solution
Level 10
May 4, 2018

(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.

smacdonald2008
Level 10
May 4, 2018

If anyone reading this thread wants to know how to work with Sling Model and Multi Field - see this article -- Adobe Experience Manager Help | Creating a HTL Repeating Data Set 6.3 Component that uses Sling Models

dangl95641558
Level 2
May 5, 2018

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. }