Passing variable value from sightly to sling model | Community
Skip to main content
Level 2
October 1, 2020
Solved

Passing variable value from sightly to sling model

  • October 1, 2020
  • 3 replies
  • 7710 views

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?

 

 

 

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 Manjunath_K

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

3 replies

Manjunath_K
Level 7
October 1, 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;

 

}

LaMind3Author
Level 2
October 1, 2020
@manjunath_k, I tried this it gives me null value
arunpatidar
Community Advisor
Community Advisor
October 1, 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

Arun Patidar
Manjunath_K
Level 7
October 1, 2020

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;

Manjunath_K
Manjunath_KAccepted solution
Level 7
October 2, 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

LaMind3Author
Level 2
October 2, 2020
@manjunath_k, Thanks, this fixed the issue. Updated int to long..