Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Passing variable value from sightly to sling model

Avatar

Level 2

I want pass list index value to sling model.

<sly data-sly-list.img="${guide.itemsList}">
<span>index:  ${imgList.index}</span>

<sly data-sly-use.multi="${'com.mycom.....MultiField' @ index=${imgList.index}' " />

This gives me null value at model. I tried to with pass with quotes , it gets passed as string.

 

Can any one help me to resolve this?

 

 

 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

6.5
1 Accepted Solution

Avatar

Correct answer by
Level 8

Hi @LaMind3 

After debugging able to find root cause for this issue, its because of datatype mismatch & datatype of itemList.index value is long. please use below updated code where datatype is changed from int to long, value will be injected as expected & it will resolve the issue.

 

Sightly:

<sly data-sly-use.multi="${'com.mycom.....MultiField' @ index = imgList.index}" />

 

 

Model Class:

@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class MultiField{

 

@Inject
private long index;

 

}

 

- Manjunath

View solution in original post

15 Replies

Avatar

Level 8

Hi @LaMind3 

Please find below mentioned corrected syntax & try with this.

 

Sightly:

<sly data-sly-use.multi="${'com.mycom.....MultiField' @ index = imgList.index ,context='number'}" />

 

 

Model Class:

@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class MultiField{

 

@Inject
private int index;

 

}

Avatar

Level 8

@LaMind3 

Try above highlighted updated code, add context='number'  & change datatype from String to int. 

Avatar

Level 2
@Manjunath_K, I tried with int in model , but it always passes 0 instead of dynamic value. I added ,context = "number" but this didnt help me ..

Avatar

Level 8

@LaMind3 

I verified this code in my local instance & it works fine. please find below debug screenshot where index variable injected with value '2'.

 

i will suggest you to cross check whether your model class is SlingHttpServletRequest adaptable, if 'yes' then check printing index value(which you are passing to model class) in p tag to make sure proper value is sent to model class.

 

index.PNG

 

Sightly:

<sly data-sly-use.multi="${'com.mycom.....MultiField' @ index = 2,context='number'}" />

 

 

Model Class:

@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class MultiField{

 

@Inject
private int index;

 

}

Avatar

Community Advisor

you are missing

@RequestAttribute(name = "index")
private String index;

 

Please check example here

https://github.com/arunpatidar02/aem63app-repo/blob/master/java/ParamModel.java



Arun Patidar

Avatar

Level 8

Hi @arunpatidar 

Using @Inject also we can access request attribute. i think @LaMind3 is missing to add SlingHttpServletRequest in adaptables so its not injecting the value.

 

@Inject
private int index;

Avatar

Level 2
@Manjunath_K, my model class is SlingHttpServletRequest adaptable, when I pass @ index = 2 it works, but when I pass imgList.index it does not take dynamic value. It always gives 0 when I make it as int, null when it is String

Avatar

Correct answer by
Level 8

Hi @LaMind3 

After debugging able to find root cause for this issue, its because of datatype mismatch & datatype of itemList.index value is long. please use below updated code where datatype is changed from int to long, value will be injected as expected & it will resolve the issue.

 

Sightly:

<sly data-sly-use.multi="${'com.mycom.....MultiField' @ index = imgList.index}" />

 

 

Model Class:

@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class MultiField{

 

@Inject
private long index;

 

}

 

- Manjunath

Avatar

Level 2
@Manjunath_K, Thanks, this fixed the issue. Updated int to long..

Avatar

Level 1

hi @Manjunath_K  i am also having similar issue,

 

when I pass the variable in html, i am getting the value.

<sly data-sly-use.productModelV2="${'com.......ProductModelV2' @fileReference='123'}" data-sly-unwrap></sly>
${productModelV2.fileReference} this prints the value.

 

but in the sling model, under post construct when I am trying to access the variable it gives null, I need the value inside the sling model.

 

Both tp and mp give null. Please help here

 

@RequestAttribute(name = "fileReference")
private String fileReference;

@PostConstruct
protected void postConstruct() {
String tp = fileReference;
String mp = getFileReference();
}
public String getFileReference() {
return fileReference;
}
 
 

Avatar

Community Advisor

Hi @HinaB1 
Please make sure model is adaptable with SlingHttpServletRequest as well

 

@Model(adaptables = SlingHttpServletRequest.class,



Arun Patidar