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

Passing variable value from sightly to sling model

LaMind3
Level 2
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
Manjunath_K
Correct answer by
Community Advisor
Community Advisor

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

11 Replies
Manjunath_K
Community Advisor
Community Advisor

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;

 

}

Manjunath_K
Community Advisor
Community Advisor

@LaMind3 

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

LaMind3
Level 2
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 ..
Manjunath_K
Community Advisor
Community Advisor

@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;

 

}

Arun_Patidar
Community Advisor
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

Manjunath_K
Community Advisor
Community Advisor

Hi @Arun_Patidar 

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;

LaMind3
Level 2
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
Manjunath_K
Correct answer by
Community Advisor
Community Advisor

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