0

I am creating a spring web app for comparing two directories and I had stored all the absolute path of the directories containing file in a ArrayList now I want to display them on the jsp page as a tree structure but I am not getting how to implement this so please can anybody help me solving this.......how can I display the paths contained in the arraylist as a tree structure on JSP.....

I am trying to print like this.....

Root


|
|----Dependencies
|        |----A
 |        |----B
 |        |----C
 |
|----Dependents
      |----D
      |----E

This is my code for controller.... from which I am sending the list of path to jsp using session variable....my absolute paths are contained in the list filepath_firstdir and I want this filepath_firstdir to be printed on jsp as a tree.........

@Controller
public class DirectorySearchController {

public File folder;
public File folder2;
public Map<String,String> filepath_firstdir = new HashMap<String,String>();
public Map<String,String> filepath_seconddir = new HashMap<String,String>();
public List<String> filepath=new ArrayList<String>();
public Map<String,List<String>> NoChange=new HashMap<String,List<String>>();
public Map<String,List<String>> change=new HashMap<String,List<String>>();
public Map<String, String> newfile=new HashMap<String, String>();
public StringBuffer hex;
public StringBuffer hex1;
public HttpSession session;
public String msg;

@RequestMapping("/directory")
public ModelAndView scanDirectory(HttpServletRequest request,HttpServletResponse response) throws NoSuchAlgorithmException, IOException
{
     String first_path=request.getParameter("uploaddir1");
     String second_path=request.getParameter("uploaddir2");

     folder = new File(first_path);
     folder2=new File(second_path);
     filepath_firstdir=listFilesForFolder(folder);

     filepath_seconddir=listFilesForFolder(folder2);


     /*String s=filepath_firstdir.toString();
     String[] tokens=s.split("####");
     for(String token:tokens)
         filepath_seconddir.add(token);*/

     session=request.getSession();
     session.setAttribute("filepath_firstdir", filepath_firstdir);
     session.setAttribute("filepath_seconddir", filepath_seconddir);

     System.out.println(filepath_firstdir);
     System.out.println(filepath_seconddir);

     Iterator itr=filepath_firstdir.entrySet().iterator();
        while(itr.hasNext())
        {
            Entry entry=(Entry) itr.next();
            Object key=entry.getKey();
            Object value=entry.getValue();
            if(filepath_seconddir.containsValue(value))
            {
                hex=MD5Encryption.convertToHashCode(key.toString());
                Iterator itr2=filepath_seconddir.entrySet().iterator();
                while(itr2.hasNext())
                {
                    Entry entryset=(Entry) itr2.next();
                    Object key2=entryset.getKey();
                    Object value2=entryset.getValue();
                    if(value2.equals(value))
                    {
                        hex1=MD5Encryption.convertToHashCode(key2.toString());
                        //System.out.println(hex);
                        //System.out.println(hex1);
                        if(hex.toString().equals(hex1.toString()))
                        {
                            List<String> NoChangevalues=new ArrayList<String>();
                            NoChangevalues.add(key.toString());
                            NoChangevalues.add(key2.toString());
                            NoChange.put(value.toString(),NoChangevalues);

                        }
                        else
                        {
                            List<String> Changevalues=new ArrayList<String>();
                            Changevalues.add(key.toString());
                            Changevalues.add(key2.toString());
                            change.put(value.toString(),Changevalues);

                        }

                    }
                }
            }
            else
                newfile.put(key.toString(), value.toString());
        }

        Iterator itr2=filepath_seconddir.entrySet().iterator();
        while(itr2.hasNext())
        {
            Entry entryset=(Entry) itr2.next();
            Object key=entryset.getKey();
            Object value=entryset.getValue();
            if(!filepath_firstdir.containsValue(value))
                newfile.put(key.toString(), value.toString());
        }

        session.setAttribute("NoChange", NoChange);
        session.setAttribute("Change", change);
        session.setAttribute("newfile", newfile);

    return new ModelAndView("success","message",msg);
}


public Map<String, String> listFilesForFolder(File folder) {

    String temp="";
    Map<String,String> filepath=new HashMap<String,String>();
    //List<String> filepath2=new ArrayList<String>();
    for (File fileEntry : folder.listFiles()) {
      if (fileEntry.isDirectory()) {

        filepath.putAll(listFilesForFolder(fileEntry));
      } else {
        if (fileEntry.isFile()) {
          temp = fileEntry.getName();
          //if ((temp.substring(temp.lastIndexOf('.') + 1, temp.length()).toLowerCase()).equals("txt"))
          {
            System.out.println("File= " + folder.getAbsolutePath()+ "\\" + fileEntry.getName());

            filepath.put(folder.getAbsolutePath()+"\\"+fileEntry.getName(),fileEntry.getName());

          }
        }

      }
    }
    return filepath;
  }}
Rishi Arora
  • 1,713
  • 3
  • 16
  • 31

1 Answers1

0

Dont pass the list of strings to jsp. Construct the tree on the serverside and send the tree there and render it. See this How to convert list of filepaths to a hireachial tree in Java

Community
  • 1
  • 1
Nadir
  • 1,369
  • 1
  • 15
  • 28