Sightly Java return types | Community
Skip to main content
October 16, 2015
Solved

Sightly Java return types

  • October 16, 2015
  • 3 replies
  • 1015 views

All the examples I have seen thus far (including official documentation) use simple return types for Java methods. In the following example taken from http://experiencedelivers.adobe.com/cemblog/en/experiencedelivers/2014/01/sightly-preview-part2.html getMyTitle returns a String :-

<div data-sly-use.comp1="com.myproject.SightlyComponent"> <h1>${comp1.myTitle}</h1> </div> public class SightlyComponent extends WCMUse  { private String myTitle; @Override public void activate() { myTitle = "My Project " + getCurrentPage().getTitle(); } public String getMyTitle() { return myTitle; } }

Is is possible to return a an Object (or interface) type from a method .. say ? :-

<div data-sly-use.person="com.mycompany.aem.Person"> <h1>${person.address.zipCode}</h1> </div>
package com.mycompany.aem; public class Person extends WCMUse  {     import com.mycompany.aem.Address;     ...          public String firstName; public String lastName; public Address address; public Address getAddress() { ... return address; } } package com.mycompany.aem;
public class Address{ public String streetName; public String zipCode; ... }

I tried something along these lines but couldn't get is to work so wondered whether there is a way of achieving it ??

Regards

Fraser.

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 Dinu_Arya

Hi Fraser,

It's possible to return an object. But make sure that the model/bean(Address) class is available to sightly java class(Person). Also you have to call(use) whatever the classes(Address) you are using in sightly java class(Person), before you use sightly java class in <component>.html. try below -

In <component>.html

<div data-sly-use="com.mycompany.aem.Address"></div>

<div data-sly-use.person="com.mycompany.aem.Person">

        <p>${person.firstName}</p>

        <p>${person.lastName}</p>

        <p>${person.address.streetName}</p>

        <p>${person.address.zipCode}</p>

</div>

I hope it will work. Also write getter methods in Address class. you can iterate list of objects as well. Make sure that you have followed java coding standards in both the classes. Let me know what error you are getting exactly .

Thanks,

AryA.

"

3 replies

Dinu_Arya
Dinu_AryaAccepted solution
Level 6
October 16, 2015

Hi Fraser,

It's possible to return an object. But make sure that the model/bean(Address) class is available to sightly java class(Person). Also you have to call(use) whatever the classes(Address) you are using in sightly java class(Person), before you use sightly java class in <component>.html. try below -

In <component>.html

<div data-sly-use="com.mycompany.aem.Address"></div>

<div data-sly-use.person="com.mycompany.aem.Person">

        <p>${person.firstName}</p>

        <p>${person.lastName}</p>

        <p>${person.address.streetName}</p>

        <p>${person.address.zipCode}</p>

</div>

I hope it will work. Also write getter methods in Address class. you can iterate list of objects as well. Make sure that you have followed java coding standards in both the classes. Let me know what error you are getting exactly .

Thanks,

AryA.

"

Feike_Visser1
Adobe Employee
Adobe Employee
October 16, 2015

Yes, try for example with a Page-object

${yourComponent.page.properties.jcr:title}

October 16, 2015

Thanks AryA and Feike. I now have Java objects that contain other objects (i.e. a graph) accessible to Sightly and am accessing fields and methods successfully.

Rest assured that the example code above was just that ... just something I typed in to illustrate the problem and not the actual code I am using ;-)

Kind Regards

Fraser.