1

我正在尝试实现 struts2-jquery-tree-plugin (https://github.com/struts-community-plugins/struts2-jquery)并且树视图不会显示。

我已经创建了动作:

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;
    }
}

然后我在 struts.xml 文件中设置它:

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

然后尝试在我的 JSP 中显示它:

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

当我检查页面的元素时,我可以看到divul,但没有列表项,如果我访问 /jsonTreeData.do,我可以看到数据:

{
    "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
        }
    ]
}

根据我分享的内容,我想这要么是我试图在我的 JSP 上呈现它的问题,要么可能 JSON 结果不是插件的理想格式,尽管我一直在关注文档.

我对 Java 没有那么丰富的经验,并且在我的生活中没有见过太多的 struts2,所以我可能会错过一些愚蠢的东西。如果您查看该插件的文档,您会发现内容不多,所以我希望这里有人可以帮助我。

4

0 回答 0