Struts JQuery Tree使用json Data分配节点特定URL

时间:2014-01-23 14:23:38

标签: json jsp struts2 struts2-jquery

我尝试了与http://code.google.com/p/struts2-jquery/wiki/TreeTag

相同的代码

生成树结构工作正常,但我想为树的每个TreeNode分配不同的URL。我如何对JSON响应做同样的事?

具有JSON数据源的树组件。 JSP代码

    <s:url var="treeDataUrl" action="json-tree-data"/>
    <sjt:tree 
            id="jsonTree" 
            href="%{treeDataUrl}"
            onClickTopics="treeClicked" 
    />

Java Action Code

@ParentPackage(value = "showcase")
public class JsonTreeData extends ActionSupport {
    private static final long serialVersionUID = -2886756982077980790L;
    private List<TreeNode> nodes = new ArrayList<TreeNode>();
    private String id = "";

    @Actions( { @Action(value = "/json-tree-data", results = { @Result(name = "success", type = "json", params = {
                    "root", "nodes" }) }) })
    public String execute() {

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

    public String getJSON() {
            return execute();
    }

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

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

TreeNode.java

    public class TreeNode implements Serializable {

    public static final String NODE_STATE_CLOSED = "closed";
    public static final String NODE_STATE_OPEN = "open";

    private Map<String, Object> attr;

    private Collection<TreeNode> children;
    private String icon;
    private String id;
    private String state = TreeNode.NODE_STATE_CLOSED;
    private String title;

    public TreeNode() {
            super();
    }

    public TreeNode(String title) {
            super();
            this.title = title;
    }

    public TreeNode(String id, String title) {
            super();
            this.id = id;
            this.title = title;
    }

    public TreeNode(String title, Collection<TreeNode> children) {
            super();
            this.title = title;
            this.children = children;
    }

    public TreeNode(String id, String title, Collection<TreeNode> children) {
            super();
            this.id = id;
            this.title = title;
            this.children = children;
    }

    public Map<String, Object> getAttr() {
            return attr;
    }

    public Collection<TreeNode> getChildren() {
            return children;
    }

    /**
     * Get the Tree Node Title
     */
    public String getData() {
            return title;
    }

    public String getIcon() {
            return icon;
    }

    public String getId() {
            return id;
    }

    public String getState() {
            return state;
    }

    public String getTitle() {
            return title;
    }

    public void setAttr(Map<String, Object> attr) {
            this.attr = attr;
    }

    /**
     * Set the Tree Node Childrens
     *
     * @param children
     */
    public void setChildren(Collection<TreeNode> children) {
            this.children = children;
    }

    /**
     * Set the Tree Node Icon
     *
     * @param icon
     */
    public void setIcon(String icon) {
            this.icon = icon;
    }

    /**
     * Set the Tree Node Id
     *
     * @param icon
     */
    public void setId(String id) {

            this.id = id;
            if (this.attr == null) {
                    attr = new HashMap<String, Object>();
            }

            if (this.attr.containsKey("id")) {
                    this.attr.remove("id");
            }
            this.attr.put("id", id);
     }

    /**
     * Set the Tree Node State open or closed
     *
     * @param state
     */
    public void setState(String state) {
            this.state = state;
    }

    /**
     * Set the Tree Node Title
     *
     * @param title
     */
    public void setTitle(String title) {
            this.title = title;
    }

    @Override
    public String toString() {
            StringBuilder builder = new StringBuilder();
            builder.append("TreeNode [id=").append(id).append(", title=").append(
                            title).append(", icon=").append(icon).append(", state=")
                            .append(state).append(", attr=").append(attr).append(
                                            ", children=").append(children).append("]");
            return builder.toString();
    }


    }

对于每个节点它生成输出

<li id="C0" class="jstree-closed jstree-last">
    <ins class="jstree-icon ui-icon ui-icon-triangle-1-e">&nbsp;</ins>
    <a href="#" class="ui-state-default">
                 <ins class="jstree-icon ui-icon ui-icon-folder-collapsed">&nbsp;</ins>
        Node C0
    </a>
</li>   

我想将每个节点特定的url分配给上面生成的相应锚标签。

感谢任何帮助,谢谢!

0 个答案:

没有答案