0
votes

I am trying to get a List of custom object of type linked list into html using Sightly. But I a unable to read them in sightly. Sample Code is pasted below:

Java Bean:

public class MiniNavBean {

    private String fPath;

    private String activeAttr;

    public MiniNavBean(String fPath, String activeAttr){
        this.fPath = fPath;
        this.activeAttr = activeAttr;
    }

    public String getFpath() {
        return fPath;
    }

    public void setFpath(String fpath) {
        this.fPath = fpath;
    }

    public String getActiveattr() {
        return activeAttr;
    }

    public void setActiveattr(String activeattr) {
        this.activeAttr = activeattr;
    }

}

Java class which extends WCMUsePojo:

public class MiniNav extends WCMUsePojo {

    private List<MiniNavBean> navList;

    MiniNavBean miniNav;

    public List<MiniNavBean> getNavList() {
        return navList;
    }

    public void setNavList(List<MiniNavBean> navList) {
        this.navList = navList;
    }

    @Override
    public void activate() {
        navList = new LinkedList<MiniNavBean>();
        fPath = "fpaths";
        activeAttr = "activeattrs;"       
        miniNav = new MiniNavBean(fpath, activeattr);
        navList.add(miniNav);
    }
}

Html file (Sightly):

<div data-sly-include="/apps/project/components/global.jsp"></div>
<div data-sly-use.mininav="com.components.MiniNav" data-sly-unwrap>
<div data-sly-list.navlist="${mininav.navList}">
   <li>
    <p>${navlist.fPath}</p>
    <p>${navlist.activeAttr}</p>
   </li>
</div>

When I am trying to execute the above code, I am able to see the linked list getting instantiated with the data in the java class. However when I am trying to display the values of the list in the front end, sightly is unable to read it.

Since the LinkedList is of CustomObject type(MiniNavBean) I suspect sightly is unable to read it as it doesn't know about this bean because we didn't refer that bean anywhere. Is there a way to fix this using sightly tags and read the data ?

2

2 Answers

2
votes

Sightly would loop over Java objects too. I don't think it is issue with Sightly. Looks like your getters are wrong. Change your bean as shown below

public class MiniNavBean {

    private String fPath;

    private String activeAttr;

    public MiniNavBean(String fPath, String activeAttr){
        this.fPath = fPath;
        this.activeAttr = activeAttr;
    }

    public String getfPath() {
        return fPath;
    }

    public void setfPath(String fPath) {
        this.fPath = fPath;
    }

    public String getActiveAttr() {
        return activeAttr;
    }

    public void setActiveAttr(String activeAttr) {
        this.activeAttr = activeAttr;
    }
}

If you do not wish to change the bean, then you can access the getters directly in the Sightly file and check if it is working fine.

<div data-sly-include="/apps/project/components/global.jsp"></div>
<div data-sly-use.mininav="com.components.MiniNav" data-sly-unwrap>
<div data-sly-list.navlist="${mininav.navList}">
   <li>
    <p>${navlist.getFpath}</p>
    <p>${navlist.getActiveattr}</p>
   </li>
</div>

EDIT: To explain more based on the comments

You cannot access the fields which are private outside the class and are usually done using the public getter methods. However, in Sightly when you use the field name after the dot operator, you are not accessing the field directly, instead it calls the corresponding getter method based on the Java specification for naming getters / setters. So as per spec, your getters and setters were wrong in the bean due to which it didn't work.

Like I mentioned above, you can change only your bean and your code will work fine. Or you can leave your bean as is and change Sightly code to get things working.

0
votes

In your example you are neither assigning a value to the navList member of MiniNav nor adding the MiniNavBean instance to it.

Add the following lines to your activate() method:

this.navList = new LinkedList<>();
this.navList.add(navList);

Also, the Java getters and HTL/Sightly accessors are not properly aligned, ie: for getFpath() you should use navlist.fpath

In case you already have those, are you getting any compile or runtime errors from HTL/Sightly?

HTL/Sightly is generally using reflection to lookup properties of objects so it does not care much about their type.