Using Easyui to implement asynchronous load tree in JSP (Consolidate struts 2)

Source: Internet
Author: User
Tags json

First JSP page has a UL used to show tree

<ul id= "Mytree" ></ul>

Load Tree

<script type= "Text/javascript" >  
    $ (' #mytree '). Tree ({     
        URL: ' treeload.action '
    });   
</script>

Configure action

<struts>  
    <package name= "Tree_json" extends= "Json-default" > <action name= "treeload" method= "  
        Treeload "class=" com.home.web.TreeAction ">  
            <result type=" json ">  
                <param name=" root "> treenodes</param>  
            </result>  
        </action>  
    </package>  
</struts>

Attention:

1.extends is a json-default that returns the JSON object format.

The name of the Param in 2.result is root, and the value in it is the JSON object to return in the action

Need to encapsulate object tree

public class TreeNode {  
    private static final long serialversionuid = 1L;  
    Private String ID;   Node ID  
    private string text;//display node text  
    private String state = "open";//Node status, ' Open ' or ' closed ', default is ' open '  
    pri Vate Boolean checked;       Indicates whether the check node is selected.  
      
    Public TreeNode () {} public  
      
    TreeNode (string ID, string text, String state, Boolean checked) {  
        this.id = ID;  
        This.text = text;  
        This.state = State;  
        this.checked = checked;  
    }  
      
    //... Omit Setxx () getxx ()  
      
}

Table structure as shown

First, query all data that parentid is null, and then determine if there are any child nodes under that node, and if so, the state is turned off

When you continue to expand the tree, the ID value is passed in, and then the child nodes of that node are queried.

Action Method implementation

Import java.sql.Connection;  
Import Java.sql.ResultSet;  
Import java.sql.SQLException;  
Import java.sql.Statement;  
Import java.util.ArrayList;  
      
Import java.util.List;  
      
Import Com.home.util.ConnectionManager; /** * Query data use JDBC to operate */public class Treeaction {private list<treenode> treenodes = new Arraylist&lt ; Treenode> (); The returned JSON data private String ID;  
        The tree component uses an ID public String treeload () {Statement STA = null;  
        ResultSet rs = null;  
            try {Connection conn = connectionmanager.getconnection ();  
            STA = Conn.createstatement ();  
            String sql = "";  
            if (id = = NULL) {//If the ID is null is the root node sql = "SELECT * from easyui_tree where parentid = '";  
            else {//query sub Node sql = "SELECT * from easyui_tree where parentid =" + ID;  
      
rs = sta.executequery (SQL);            while (Rs.next ()) {String id = rs.getstring ("id");  
                String name = rs.getstring ("name");  
                TreeNode node = new TreeNode ();  
                Node.setid (ID);  
                Node.settext (name);  
                Node.setchecked (FALSE);  
                Determine if there are child nodes, if there are closed otherwise open if (Ischildrennode (ID)) {node.setstate ("closed");  
                }else{node.setstate ("open");  
            } treenodes.add (node);  
        //Close All Resources connectionmanager.closeall (RS, STA, conn);  
        catch (SQLException e) {e.printstacktrace ();  
    Return to "success";  
        /** * To determine if there is a child node * * @return/public boolean ischildrennode (String ID) {  
        Boolean flag = false;  
        Statement sta = null;ResultSet rs = null;  
            try {Connection conn = connectionmanager.getconnection ();  
            STA = Conn.createstatement ();  
            String sql = "SELECT * from easyui_tree where parentid =" + ID;  
            rs = sta.executequery (SQL);  
            while (Rs.next ()) {flag = true;  
        //Close All Resources connectionmanager.closeall (RS, STA, conn);  
        catch (SQLException e) {e.printstacktrace ();  
    return flag;  
    Public list<treenode> Gettreenodes () {return treenodes;  
    public void Settreenodes (list<treenode> treenodes) {this.treenodes = TreeNodes;  
    Public String GetId () {return id;  
    public void SetId (String id) {this.id = ID; }  
      
}

The method of acquiring connection is encapsulated.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/ajax/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.