struts2-jquery-tree-plugin: TreeView won't show

279 views Asked by At

I'm trying to implement struts2-jquery-tree-plugin (https://github.com/struts-community-plugins/struts2-jquery) and the treeview just won't show.

I've created the action:

import java.util.ArrayList;
import java.util.List;

import com.jgeppert.struts2.jquery.tree.result.TreeNode;

public class JsonTreeViewAction {

    private List<TreeNode> nodes = new ArrayList<TreeNode>();
    private String id;
    
    public String execute() {

        System.out.println("ACTION");
        TreeNode nodeA = new TreeNode();
        nodeA.setId("A" + id);
        nodeA.setTitle("Node A" + id);
        nodeA.setState(TreeNode.NODE_STATE_CLOSED);

        TreeNode nodeB = new TreeNode();
        nodeB.setId("B" + id);
        nodeB.setState(TreeNode.NODE_STATE_CLOSED);
        nodeB.setTitle("Node B" + id);

        TreeNode nodeC = new TreeNode();
        nodeC.setId("C" + id);
        nodeC.setState(TreeNode.NODE_STATE_CLOSED);
        nodeC.setTitle("Node C" + id);

        nodes.add(nodeA);
        nodes.add(nodeB);
        nodes.add(nodeC);
        return "ok";
    }

    public String getJSON() {
        return execute();
    }
    
    public List<TreeNode> getNodes() {
        return nodes;
    }

    public void setId(String id) {
        this.id = id;
    }
}

Then I set it up in the struts.xml file:

<package name="default" namespace="/" extends="bsad-default,json-default">
    <action name="jsonTreeData" class="com.mycompany.JsonTreeViewAction">
        <result name="ok" type="json">
        </result>
    </action>
</package>

And then tried to display it in my JSP:

<sjt:tree
    id="jsonTree1"
    jstreetheme="apple"
    href="%{treeDataUrl}"
    rootNode="nodes"
    nodeTitleProperty="title"
    nodeIdProperty="id"
    childCollectionProperty="children"
    onClickTopics="treeClicked"
    targets="result"
/>

When I inspect the page's elements, I can see a div and a ul, but no list items, and if I access /jsonTreeData.do, I'm able to see the data:

{
    "JSON":"ok",
    "nodes":[
        {
            "attr":{
                "id":"Anull"
            },
            "children":null,
            "data":{
                "title":"Node Anull"
            },
            "icon":null,
            "id":"Anull",
            "state":"closed",
            "title":"Node Anull",
            "type":null
        },
        {
            "attr":{
                "id":"Bnull"
            },
            "children":null,
            "data":{
                "title":"Node Bnull"
            },
            "icon":null,
            "id":"Bnull",
            "state":"closed",
            "title":"Node Bnull",
            "type":null
        },
        {
            "attr":{
                "id":"Cnull"
            },
            "children":null,
            "data":{
                "title":"Node Cnull"
            },
            "icon":null,
            "id":"Cnull",
            "state":"closed",
            "title":"Node Cnull",
            "type":null
        },
        {
            "attr":{
                "id":"Anull"
            },
            "children":null,
            "data":{
                "title":"Node Anull"
            },
            "icon":null,
            "id":"Anull",
            "state":"closed",
            "title":"Node Anull",
            "type":null
        },
        {
            "attr":{
                "id":"Bnull"
            },
            "children":null,
            "data":{
                "title":"Node Bnull"
            },
            "icon":null,
            "id":"Bnull",
            "state":"closed",
            "title":"Node Bnull",
            "type":null
        },
        {
            "attr":{
                "id":"Cnull"
            },
            "children":null,
            "data":{
                "title":"Node Cnull"
            },
            "icon":null,
            "id":"Cnull",
            "state":"closed",
            "title":"Node Cnull",
            "type":null
        }
    ]
}

Based on what I've shared, I imagine it's either a matter of how I'm trying to render it on my JSP, or maybe the JSON result is not in the ideal format for the plugin, although I've been following the docs.

I'm not that experienced with Java and have not seen much struts2 in my life, so I might be missing something silly. If you check out the docs for the plugin, you'll see that there is not a lot of content, so I was hoping someone here could help me.

0

There are 0 answers