How to pass multiple parameters to sling model from sightly | Community
Skip to main content
Level 2
Dec 9, 2020 3:54 PM UTC
Solved

How to pass multiple parameters to sling model from sightly

I have written sling model exporter . I need to call this by passing 2 parameters fileReference & imgParam

<sly data-sly-use.slingmodel="com.myproj.core.models.TestSlingModel"  @ fileReference ='/some/path/to/file' , @ imgParam='image'/>

 

In sling model (@Model(adaptables = {Resource.class, SlingHttpServletRequest.class})

I have written:

@14766979
@ValueMapValue
private String fileReference;

 

@14766979
@ValueMapValue
private String imgParam;

 

I am not able to pass parameters properly. Can anyone tell me if this is right way of passing multiple parameters?

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Manjunath_K
Manjunath_KAccepted solution
Level 7
Dec 9, 2020 6:09 PM UTC

Hi @lamind3 ,

Try passing multiple parameters as mentioned below, for sample i have hardcoded parameter as string values here & replace these hardcoded value by dynamic values. please make sure datatype of dynamic value getting passed in sightly is same as datatype defined for @Inject variable in Sling Model class. 

 

Sightly:

<sly data-sly-test.file="/some/path/to/file" data-sly-test.image="image">
<sly data-sly-use.helloWorldModel="${'com.project.HelloWorldModel' @fileReference=file, imgParam=image}"/>
</sly>

 

Sling Model Class:

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

@Inject
private String fileReference;

@Inject
private String imgParam;

 

Hope this helps!

5 replies

Manjunath_K
Manjunath_KAccepted solution
Level 7
Dec 9, 2020 6:09 PM UTC

Hi @lamind3 ,

Try passing multiple parameters as mentioned below, for sample i have hardcoded parameter as string values here & replace these hardcoded value by dynamic values. please make sure datatype of dynamic value getting passed in sightly is same as datatype defined for @Inject variable in Sling Model class. 

 

Sightly:

<sly data-sly-test.file="/some/path/to/file" data-sly-test.image="image">
<sly data-sly-use.helloWorldModel="${'com.project.HelloWorldModel' @fileReference=file, imgParam=image}"/>
</sly>

 

Sling Model Class:

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

@Inject
private String fileReference;

@Inject
private String imgParam;

 

Hope this helps!

Translate
LaMind3Author
Level 2
Dec 10, 2020 10:40 AM UTC
Thanks @manjunath_k , will try this out
Translate
Anudeep_Garnepudi
New Participant
Dec 9, 2020 4:04 PM UTC

Hi @lamind3 

There is no need to pass parameters to Sling Model, whatever you can get in HTL, you can get the same in Sling Model as well. If the values are hardcoded, directly hardcode in Sling Model itself.

Try this: <sly data-sly-use.slingmodel="${'com.myproj.core.models.TestSlingModel' @ fileReference ='/some/path/to/file' , imgParam='image'}" />

Model:

@Inject

String fileReference;

@Inject

String imgParam;

AG

Translate
LaMind3Author
Level 2
Dec 9, 2020 4:08 PM UTC
Hi Anudeep, I need to call this from different components so parameter values will change, I can not hard code it.
Translate