treeusingdatabasedao.jsp

来自「部门结构树」· JSP 代码 · 共 133 行

JSP
133
字号

<%@ page import="com.jenkov.prizetags.tree.itf.*" %>
<%@ page import="java.io.File"%>
<%@ page import="com.jenkov.prizetags.tree.impl.*"%>
<%@ page import="javax.sql.DataSource"%>
<%@ page import="java.sql.SQLException"%>
<%@ page import="java.io.PrintWriter"%>
<%@ page import="java.sql.Connection"%>
<%@ page import="java.sql.DriverManager"%>
<%@ taglib uri="/WEB-INF/treetag.tld" prefix="tree" %>
<%@ taglib uri="/WEB-INF/requesttags.tld" prefix="request" %>
<%
    if(session.getAttribute("example") == null){

        //creating a simple tree model instance
        ITree       tree          = new Tree();
        tree.setSingleSelectionMode(true);
//        tree.setTreeDao(new FileDao(new File("./webapps/prizetagsdemo")));

        DataSource dataSource = new DataSource(){
            public int getLoginTimeout() throws SQLException {
                return 0;
            }

            public void setLoginTimeout(int seconds) throws SQLException {

            }

            public PrintWriter getLogWriter() throws SQLException {
                return null;
            }

            public void setLogWriter(PrintWriter out) throws SQLException {

            }

            public Connection getConnection() throws SQLException {
                try {
                    Class.forName("org.hsqldb.jdbcDriver");
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                return DriverManager.getConnection("jdbc:hsqldb:file:database/tree/tree", "sa", "");
            }

            public Connection getConnection(String username, String password) throws SQLException {
                return null;
            }
        };

        TreeTableMapping mapping = new TreeTableMapping("tree", "node_id", "parent_id", null, "name", "type", "tooltip");

        tree.setTreeDao(new DatabaseDao(dataSource, mapping));

        //storing the tree model instance in the session.
        //this is where the tags get the tree model from later.
        session.setAttribute("example", tree);
//        request.setAttribute("example", tree);
    }

%>
<html>
<head>
    <title>Tree Tag - Single Page - (C) 2004 Jenkov Development</title>
    <link rel="stylesheet" href="/prizetagsdemo/stylesheet.css" type="text/css">
</head>


<body>
<h2>Tree Using ITreeDao</h2>

This JSP page demonstrates how to use the ITreeDao to dynamically (and lazily) load the nodes of
a tree. The demo uses a FileDao which reads the nodes from the file system.

<br/><br/>
You can implement your
own ITreeDao to read nodes from whereever you need to... database, XML DOM trees, JNDI etc.
The ITreeDao interface is simple. It only contains two methods.


<br/><br/>

<table cellspacing="0" cellpadding="5" style="border: 1 solid black;">

<tr><td valign="top">

        <%-- Generating the Tree HTML --%>
        <table cellspacing="0" cellpadding="0" border="0">
        <tree:tree tree="example" node="example.node" includeRootNode="true">
            <tr><td
            ><table cellspacing="0" cellpadding="0" border="0">
            <tr><td><tree:nodeIndent    node="example.node" indentationType="type"><tree:nodeIndentVerticalLine indentationType="type" ><img src="../images/verticalLine.gif"></tree:nodeIndentVerticalLine><tree:nodeIndentBlankSpace   indentationType="type" ><img src="../images/blankSpace.gif"></tree:nodeIndentBlankSpace></tree:nodeIndent></td>
            <tree:nodeMatch    node="example.node" expanded="false" hasChildren="true"  isLastChild="false"><td><a href="<request:requestUri/>?expand=<tree:nodeId node="example.node"/>"><img src="../images/collapsedMidNode.gif" border="0"></a><img src="../images/closedFolder.gif"></td></tree:nodeMatch>
            <tree:nodeMatch    node="example.node" expanded="true"  hasChildren="true"  isLastChild="false"><td><a href="<request:requestUri/>?collapse=<tree:nodeId node="example.node"/>"><img src="../images/expandedMidNode.gif"  border="0"></a><img src="../images/openFolder.gif"></td></tree:nodeMatch>
            <tree:nodeMatch    node="example.node" expanded="false" hasChildren="true"  isLastChild="true" ><td><a href="<request:requestUri/>?expand=<tree:nodeId node="example.node"/>"><img src="../images/collapsedLastNode.gif"  border="0"></a><img src="../images/closedFolder.gif"></td></tree:nodeMatch>
            <tree:nodeMatch    node="example.node" expanded="true"  hasChildren="true"  isLastChild="true" ><td><a href="<request:requestUri/>?collapse=<tree:nodeId node="example.node"/>"><img src="../images/expandedLastNode.gif" border="0"></a><img src="../images/openFolder.gif"></td></tree:nodeMatch>
            <tree:nodeMatch    node="example.node" expanded="false" hasChildren="false" isLastChild="false"><td><img src="../images/noChildrenMidNode.gif"><img src="../images/nonFolder.gif"></td></tree:nodeMatch>
            <tree:nodeMatch    node="example.node" expanded="false" hasChildren="false" isLastChild="true" ><td><img src="../images/noChildrenLastNode.gif"><img src="../images/nonFolder.gif"></td></tree:nodeMatch>

            <td valign="top">
            <tree:nodeMatch node="example.node" selected="true"><span style="Font-Size: 12px;"><b><tree:nodeName      node="example.node"/></b></span></tree:nodeMatch>
            <tree:nodeMatch node="example.node" selected="false"><a href="<request:requestUri/>?select=<tree:nodeId node="example.node"/>"><span style="Font-Size: 12px;"><tree:nodeName      node="example.node"/></span></a></tree:nodeMatch>

            </td>
            </tr>
            </table></td></tr>
        </tree:tree>
        </table>
    </td>
    <td valign="top" style="border-left: 1 solid black;">
        Selected Node Detail:<br/><br/>

        <%
            ITree tree = (ITree) session.getAttribute("example");
            ITreeNode node = new TreeNode(); //empty node, displayed when the tree isn't initialized yet.
            if(tree != null && request.getParameter("select") != null){
                node = tree.findNode(request.getParameter("select"));
            }
        %>
        <table cellspacing="0" cellpadding="5">
            <tr><td>Id  </td><td><%=node.getId()%></td></tr>
            <tr><td>Name</td><td><%=node.getName()%></td></tr>
            <tr><td>Type</td><td><%=node.getType()%></td></tr>
        </table>


    </td>
</tr>
</table>

</body>

</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?