Expand my Community achievements bar.

unable to get the List of objects using Java Use API and Sightly(Htl)

Avatar

Level 4

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 ?

16 Replies

Avatar

Employee

In your sample I don't see any assignments to the List<> variable..

private List<MiniNavBean> navList;

Avatar

Employee

On the sightly syntax, you can also use the following:

<div data-sly-use.mininav="com.components.MiniNav" data-sly-list.navlist="${mininav.navList}">

No need for the global.jsp, and you can combine data-sly attributes within an element

Avatar

Level 4

Does it matter if I combine data-sly-attributes within an element or not. I need to display the values of the mainnav bean object which is part of linked list. 

Avatar

Employee

Just tried your sample with a LinkedList, and works perfectly on my end.

Share your sample that doesn't work, to make sure we don't hit basic typos for example.

Avatar

Level 4

Feike Visser wrote...

Just tried your sample with a LinkedList, and works perfectly on my end.

Share your sample that doesn't work, to make sure we don't hit basic typos for example.

 

Did you try the LinkedList of type custome object i.e. new LinkedList<MainNavBean>()  or is it a linkedlist of type String i.e. new LinkedList<String>(). It works fine with linkedlist of type String. The issue is only with the list of custom objects. Sightly is unable to find that custom object I guess. If you observe my code, I have a linked list of tyoe MainNavBean Object. That is where the issue is. Its unable to read that custom object.

Avatar

Employee

Here my sample:

package adobe.summit.lasvegas.core;

 

import java.util.LinkedList;

import java.util.List;

 

import com.adobe.cq.sightly.WCMUsePojo;

 

public class MiniNav extends WCMUsePojo {

 

    private List<MiniNavBean> navList = new LinkedList<MiniNavBean>();

 

    MiniNavBean miniNav;

 

    public List<MiniNavBean> getNavList() {

        return navList;

    }

 

    public void setNavList(List<MiniNavBean> navList) {

        this.navList = navList;

    }

 

@Override

    public void activate() {

 

        miniNav = new MiniNavBean("fpaths", "activeattrs");

 

        navList.add(miniNav);

 

    }

 

}

 

HTL:

<div data-sly-use.mininav="adobe.summit.lasvegas.core.MiniNav" data-sly-list.navlist="${mininav.navList}">
                <li>
                <p>${navlist.activeattr}</p>
                </li>

</div>

Avatar

Level 4

My code is similar to yours, but wondering why is it not working on my end:
 

Code sample:

 

public class MiniNav extends WCMUsePojo {


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


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


    @Override
    public void activate() {
        
        navList = new LinkedList<MiniNavBean>();
        MiniNavBean miniNavObj = new MiniNavBean("fpath", "activeattr", "scptitle");
        navList.add(miniNavObj);
        
    }

}

 

Sighly:

<div data-sly-include="/apps/<application>/components/global.jsp"></div>
<div data-sly-use.mininav="com.xxx.totalinsight.components.MiniNav" data-sly-list.navlist="${mininav.navList}" data-sly-unwrap>
    <div class="content center">
        <div class="itemsList">
            <ul>
                <li>
                    <p>fpath : ${navlist.fPath}</p>
                    <p>activeAttr: ${navlist.activeAttr}</p>
                </li>
            </ul>
        </div>
    </div>
</div>

Avatar

Level 4

package com.xxx.totalinsight.components;

public class MiniNavBean {

    private String fPath;
    
    private String activeAttr;
    
    private String scpTitle;
    
    public MiniNavBean(String fPath, String activeAttr,  String scpTitle){
        
        this.fPath = fPath;
        this.activeAttr = activeAttr;
        this.scpTitle = scpTitle;
    }
    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;
    }

    public String getScptitle() {
        return scpTitle;
    }

    public void setScptitle(String scptitle) {
        this.scpTitle = scptitle;
    }
    

}

Avatar

Employee

Looking at your bean on your HTL expressions, they don't match...

you have .fPath and activeAttr

and you have getFpath() and getActiveattr()

 

 <p>fpath : ${navlist.fPath}</p>
 <p>activeAttr: ${navlist.activeAttr}</p>

Avatar

Level 4
Thanks a lot. It worked now. I was expecting it to work with the variable name itself,didnt know we need to call the getter too in sightly. To refer the linkedlist in sightly I didn't call any getter but I just called the navList object directly as "${mininav.navList}">. But dont know why do we need to call the getter for the variables in bean. 
     
Thanksa lot. It worked now. I was expecting it to work with the variable name itself,didnt know we need to call the getter in sightly. To refer the linkedlist in sightly I didn't call any getter but I just called the navList object directly. But dont know why do we need to call the getter for the variables in bean. 

Avatar

Level 4

sorry this is a sample code and I have forgot to add those changes. I have made the chnages now to refer the list

Avatar

Employee

Can you upload the maven project so I can try this on an instance?

Avatar

Level 10

See this article that shows you the syntax - also be sure to use WCMUsePojo:

https://helpx.adobe.com/experience-manager/using/htl_61.html

Hope this helps...