Passing variable value from sightly to sling model

LaMind3

01-10-2020

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?

 

 

 

AEM 6.5.0

Accepted Solutions (1)

Accepted Solutions (1)

Manjunathh

01-10-2020

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

Answers (2)

Answers (2)

Arun_Patidar

MVP

01-10-2020

you are missing

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

 

Please check example here

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

Manjunathh

01-10-2020

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;

 

}