Pass an object as a parameter to sling model/wcmusepojo from sightly(HTL) | Community
Skip to main content
jaibalaji
October 7, 2020
Solved

Pass an object as a parameter to sling model/wcmusepojo from sightly(HTL)

  • October 7, 2020
  • 4 replies
  • 5029 views

Hi,

I'm converting a JSP into sightly for a component. In that component they are setting an object as a request attribute to be used in another component. We cannot set request attributes in sightly right? So I'm trying to send this object as a parameter to a sling model/wcmusepojo to set the request attribute there but I think we can only pass string values as arguments to slingmodels/wcmusepojo right? Is there any example of passing an object as a parameter to slingmodel/wcmusepojo? 
Thanks!

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 VeenaK

check out this similar query posted in the community that should help your problem.

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-pass-parameters-to-sling-modal-from-sightly-component/qaq-p/292914

4 replies

Jineet_Vora
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
October 7, 2020

Hello @jaibalaji,

You can pass the parameters in Sightly (HTL) to your Java model using the Java Use-API: https://docs.adobe.com/content/help/en/experience-manager-htl/using/htl/use-api-java.html#passing-parameters

Also, if applicable, you can use the Java Global objects like 'request': https://docs.adobe.com/content/help/en/experience-manager-htl/using/htl/global-objects.html#java-backed-objects

 

Jineet

Umesh_Thakur
Community Advisor
Community Advisor
October 7, 2020

You can not set request attribute from sightly but you can get it.

And also you can not pass a pure java object except String and some other like that so if you need any backend operation like setting request object you need to do it java only before it comes to the sightly.

if you still need to pass the parameter, you can do ti like that:

Sightly:

<sly data-sly-use.someObject="${'com.demo.core.models.DemoModel' @propertyOne='PropertyOne', propertyTwo='PropertyTwo'}>

Sling Model:

@Model(adaptables = { SlingHttpServletRequest.class })

public class DemoModel {

 

@Inject

private String propertyOne;

 

@Inject

private String propertyTwo;

 

//getter and setter

}

 

WCMUsePojo:

 

public class DemoUse extends WCMUsePojo {

    @9944223

    public void activate() throws Exception {

        String propertyOne = get("propertyOne", String.class);

        String propertyTwo = get("propertyTwo", String.class);

              }

}

NOTE: update the name of the wcmusepojo accordingly.

this will help.

Regards

Umesh Thakur

 

jaibalaji
jaibalajiAuthor
October 7, 2020
Thanks but can I pass a java object as well instead of string and do this -> get("propertyOne", JavaClassName.class); to get the object?
arunpatidar
Community Advisor
Community Advisor
October 7, 2020
Arun Patidar
jaibalaji
jaibalajiAuthor
October 7, 2020
Thanks but the problem is not getting the request object but passing an object as an argument to sling model/wcmusepojo
VeenaKAccepted solution
Level 4
October 8, 2020