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?
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
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
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;
}
Views
Replies
Total Likes
Views
Replies
Total Likes
Try above highlighted updated code, add context='number' & change datatype from String to int.
Views
Replies
Total Likes
Views
Replies
Total Likes
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.
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;
}
Views
Replies
Total Likes
you are missing
@RequestAttribute(name = "index")
private String index;
Please check example here
https://github.com/arunpatidar02/aem63app-repo/blob/master/java/ParamModel.java
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;
Views
Replies
Total Likes
Views
Replies
Total Likes
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
Views
Replies
Total Likes
You’re welcome
Views
Replies
Total Likes
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;
}
Views
Replies
Total Likes
Hi @HinaB1
Please make sure model is adaptable with SlingHttpServletRequest as well
@Model(adaptables = SlingHttpServletRequest.class,
Views
Replies
Total Likes
yeah, that's been taken care, still null
Views
Replies
Total Likes
Hi @HinaB1
Then it should be working, please check the working examples here:
Views
Replies
Total Likes