JSP Tree Structure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • prasath03
    New Member
    • Jun 2007
    • 30

    JSP Tree Structure

    Hi,

    I am struggling with the following problem. Can somebody pls help me?.

    I have a file name and have a folder File f1= new File("path of the folder");. The folder is a complicated folder structure i.e. contains various files and sub folders which contains further subfolders etc. I want to show the whole folder structure(like tree structure). But, the following code shows only the "WebContent " structure.Pleas e find below the following code:

    Code:
    File f1 = new File("D:/eclipse/workspace/cms/WebContent");
    File[] contents = f1.listFiles();
    for (int j=0; j<contents.length; j++) 
    {
    	
    	if(contents[j].isDirectory())
    	{
    		out.println("Directory : "+contents[j].getName()+"<br/>");
    	}
    	else
    	{
    		out.println("Files :"+contents[j].getName()+"<br/>");
    	}
    	
    }

    If anybody knows the answer, please let me know....

    Thanks in Advance,



    V. Prasath
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    make a method from your source, pass as parameter the path (that what you hardcoded in f1), and then call your method recursively. That means, if it's a directory, call yourself with new path parameter, made up of old path+current directory name.

    Comment

    • prasath03
      New Member
      • Jun 2007
      • 30

      #3
      Hi,

      Thanks for your reply....

      How can i make a method in jsp? I don't know how to make a method for this kind of code. Please let me know if you know....


      Thanks in Advance.

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        Ok, herewith I let you know that I know. :-)

        Classes and methods are the basics of Java programming. If you don't know how to define a method with parameters, then do Sun's Java tutorial first to get the basic knowledge.

        If you know how to write a method, but you don't know how to do that in JSP, read the jsp tutorials. For a quick start, I'll give you an example:
        Code:
        <%!
        private String printFolder(String fullFolderName)
        {
         return "printFolder was called with: " + fullFolderName;
        }
        %>
        
        Now I am calling my method. It returns:
        <%= printFolder("root") %>

        Comment

        Working...