Newsletter sign-up
View all newsletters

Sign up for our Enterprise Java Newsletter

Enterprise Java
JavaWorld Daily Brew

JSP Treeview


Hi Friends,

I want to create the simple tree view which display the

database fields as tree structure in jsp.I have to display the

database field values for example, at first i have to dispaly

clients tree, if i click the client i have to display the

projects of the client, etc. I want to use simple jsp code not

the using jsp custom tags. I have to dispaly a tree from

database then inside value, etc in tree structure.I dont want

to use JSTL or EL. Just i want to show Root Node, Parent Node,

Child Node from mysql database using simple jsp. I have pasted

the jsp code regarding this, but this is complex to apply to my

requirement. If anyone change this code and send me the simple

code with database access with treeview its nice.

Thanks in advance.

Regards,
Mike

<?php
@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*,java.util.StringTokenizer,java.util.*,java.text.*" errorPage=""
?>

<?php

         
             Connection conn
= null;
             Class.
forName("com.mysql.jdbc.Driver");

        
try{
        
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/treeview","root","root");
       
System.out.println("conn is " + conn);
        
String tempNodeID=nullconv(request.getParameter("NodeID"));
       
System.out.println("tempNodeID is " +tempNodeID);
        
int tempdownline=nullIntconv(nullconv(request.getParameter("downline")));
        
System.out.println("tempdownline is " +tempdownline);
        
setConnection(conn,out,tempNodeID,tempdownline);
        
System.out.println("after setConnection " );

         }
        
catch(Exception e)
         {
          
e.printStackTrace();
         }
?>

<?php
!
public String nullconv(String str)
{
   if(
str==null)
     
str="";
   else if(
str.equalsIgnoreCase("null"))
     
str="";
   else if((
str.trim()).equals(""))
     
str="";
   return
str;
}
public int nullIntconv(String inv)
   {
      if(
inv==null)
      {
        
inv="0";
      }
      else if((
inv.trim()).equals("null"))
      {
        
inv="0";
      }
      else if(
inv.equals(""))
      {
        
inv="0";
      }
      
      return
Integer.parseInt(inv);
   }


     
Connection con= null;   

     
PreparedStatement pstmt = null;
     
PreparedStatement pstmt1 = null;

     
ResultSet rs = null;
     
ResultSet rs1 = null;

     
JspWriter out=null;
     
String   NodeID ="1";
     
int downline = 0;
      
  
public void setConnection(Connection con,JspWriter ot,String rq,int dwline)
   {
     
this.con=con;
     
try{
     
this.out=ot;
     
NodeID=rq;
     
downline=dwline;
    
       if(
NodeID.equals(""))
         {
         
NodeID="1"///////////////////// change this according to your requirement
        
}
         if(
downline==0)
         {
         
downline=9;   ////////////////////// this is for advance requirement should use according to your requirement
        
}
      }
     
catch(Exception e)
      {
        
System.out.println("IO error :"+e);
      }
    
    }
   
   
  
public void outputJavascriptForRoot()
   {
            
     
String queryString =null;
      
     
queryString ="SELECT NodeID, Name, IsFolder, ParentID, Link,client, project, level FROM nodestable WHERE (NodeID='" + NodeID +"')";
     
try{
      
     
pstmt=con.prepareStatement(queryString);
     
rs=pstmt.executeQuery();
      
     
// It should return one and one only
     
while (rs.next())
      {
       
out.print("foldersTree = gFld('<i>tree</i>')" + "\n");
       
out.print("web" + rs.getString("NodeID") + " = insFld(foldersTree, gFld('" + rs.getString("project")  + "','test_tree.jsp?distibutorid=" + rs.getString("NodeID") + "&downline="+downline+"'))" + "\n");
       
outputJavascriptForSubFolder(Integer.parseInt(rs.getString("NodeID")), rs.getString("project"),"web" + rs.getString("NodeID"), rs.getString("Link"), downline);
        }
      
     
// close the DataReader and Connection
      
      
   
     
}
     
catch(SQLException ex)
      {
       
System.out.println("sqll eroror :"+ex);
       }
      
catch(Exception e)
       {
         
System.out.println("error :"+e);
       }
   }
   
   
public void outputJavascriptForSubFolder(int folderId, String Name, String fName, String Link, int downline)
   {
      
  
try{
     
PreparedStatement psQueryString = null;
     
ResultSet rsqueryString = null;
      
String queryString1 ="SELECT NodeID, Name, IsFolder, ParentID, client, project, Link FROM nodestable WHERE ((ParentID=" + folderId + ") AND (IsFolder=1) AND level<=" + downline + ") ORDER BY project";
     
psQueryString=con.prepareStatement(queryString1);
     
rsqueryString=psQueryString.executeQuery();
      
      
     
// iterate through the rows in the Nodes table
        
while(rsqueryString.next())
         {
        
out.print("web" + rsqueryString.getString("NodeID") + "=insFld(" + fName + ",gFld('" + rsqueryString.getString("project") + "[" + rsqueryString.getString("NodeID") + "]','test_tree.jsp?NodeID="+rsqueryString.getString("NodeID")+"&downline="+downline+"'))  " + "\n" );
        
outputJavascriptForSubFolder(Integer.parseInt(rsqueryString.getString("NodeID")),rsqueryString.getString("project"),"web" + rsqueryString.getString("NodeID"),rsqueryString.getString("Link"),downline);
         }
     }
  
catch(Exception e)
      {
       
System.out.println(e);
      }
   }
 
?>

Tree from database

BODY {background-color: white}
TD {font-size: 10pt;
font-family: verdana,helvetica;
text-decoration: none;
white-space:nowrap;}
A {text-decoration: none;
color: black}

USETEXTLINKS = 1
STARTALLOPEN = 1
PRESERVESTATE = 1
ICONPATH = ''
HIGHLIGHT = 1

<?php

outputJavascriptForRoot
();
?>

// Load a page as if a node on the tree was clicked (synchronize frames)
// (Highlights selection if highlight is available.)
function loadSynchPage(xID)
{
var folderObj;
docObj = parent.treeframe.findObj(xID);
docObj.forceOpeningOfAncestorFolders();
parent.treeframe.clickOnLink(xID,docObj.link,'basefrm');

//Scroll the tree window to show the selected node
//Other code in these functions needs to be changed to work with
//frameless pages, but this code should, I think, simply be removed
if (typeof parent.treeframe.document.body != "undefined") //scroll doesn work with NS4, for example
parent.treeframe.document.body.scrollTop=docObj.navObj.offsetTop
}

Example For Treeview

initializeDocument()
//Click the Parakeet link
loadSynchPage(11)

A tree for site navigation will open here if you enable JavaScript in your browser.

Your rating: None Average: 2 (1 vote)

Why?

Why are all the code snippets wrapped in php tags?

Dude, You can google that

Dude, You can google that question of your's, It would be more appropriate I believe.
instant degree

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <p> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br /> <br> <strike>
  • Lines and paragraphs break automatically.
  • Use <!--pagebreak--> to create page breaks.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
Just checking to see if you're an actual person rather than a spammer. Sorry for the inconvenience.