我正在创建一个用于比较两个目录的spring web应用程序,并且我已经将包含文件的目录的所有绝对路径存储在ArrayList中,现在我想在jsp页面上将它们显示为树结构但是我没有得到如何为了实现这一点,请任何人帮我解决这个问题.......如何在JSP上显示arraylist中包含的路径作为树结构......
我正试图像这样打印.....
Root
|
|----Dependencies
| |----A
| |----B
| |----C
|
|----Dependents
|----D
|----E
这是我的控制器代码....我从中使用会话变量向jsp发送路径列表....我的绝对路径包含在列表 filepath_firstdir 中,我想要这个filepath_firstdir作为树打印在jsp上.........
@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;
}}
答案 0 :(得分:0)
不要将字符串列表传递给jsp。在服务器端构造树并在那里发送树并进行渲染。看到这个 How to convert list of filepaths to a hireachial tree in Java