填充有文件名并由 servlet 设置为请求属性的映射在 JSP 中显示为空
Map filled with file names and set as request attribute by servlet appears empty in JSP
我正在为一个项目(学术)创建一个博客系统。我有一个问题,我需要将 map
(LinkedHashMap
) 传递给 jsp
文件。但是 浏览器显示什么都没有 。这是我的代码:
public void doService (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String path = "localhost:8080/YouBlogger/Posts/";
File dir = new File(path);
File [] files = dir.listFiles();
LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
Arrays.sort(files, new Comparator<Object>(){
public int compare(Object o1, Object o2) {
return compare( (File)o1, (File)o2);
}
private int compare( File f1, File f2){
long result = f2.lastModified() - f1.lastModified();
if( result > 0 ){
return 1;
} else if( result < 0 ){
return -1;
} else {
return 0;
}
}
});
for(int i=0 ; i < 10 ; i++){
map.put(files[i].getName(), files[i].getPath());
}
request.setAttribute("map", map);
RequestDispatcher dispatcher = request.getRequestDispatcher("/welcome.jsp");
dispatcher.forward(request, response);
}
我 100% 确定错误出在这段代码中,因为当我对 map
、jsp 进行硬编码时会显示其中的数据。这有什么问题吗?大概是 path
??
编辑:
这是我的 jsp 代码:
<body>
<div id = "Header">
<h1>You Blogger</h1>
</div>
<div id = "data">
<c:forEach var="country" items="${map}">
${country.key} + ${country.value}
<br/>
</c:forEach>
<form action="new_post" method = "POST">
<input type = "submit" value = "Add A New Post" ></input>
</form>
</div>
</body>
The project is running on apache tomcat 8.0 and I'm using eclipse Luna for developing.
我认为问题出在您代码的第 2 行和第 3 行:
String path = "localhost:8080/YouBlogger/Posts/";
File dir = new File(path);
这将做的是查看目录 c:/localhost:8080/YouBlogger/Posts/
并列出那里的文件,因为这个目录可能不存在于您的驱动器 c: 上,所以它 return 什么也不会。如果你想列出远程机器上的文件,你需要使用一些 HTTP 客户端或在那里放置现有目录的路径。
File
构造函数需要一个真实的路径来访问本地目录。所以你不应该使用 localhost:8080
访问它,而应该使用下面的真实路径访问目录
String path="/home/test/apache/webapp/projectname/YouBlogger/Posts/"
如果您不想对路径进行硬编码,您可以使用 request.getSession().getServletContext().getRealPath("/")
获取网络服务器目录的真实路径
我正在为一个项目(学术)创建一个博客系统。我有一个问题,我需要将 map
(LinkedHashMap
) 传递给 jsp
文件。但是 浏览器显示什么都没有 。这是我的代码:
public void doService (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String path = "localhost:8080/YouBlogger/Posts/";
File dir = new File(path);
File [] files = dir.listFiles();
LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
Arrays.sort(files, new Comparator<Object>(){
public int compare(Object o1, Object o2) {
return compare( (File)o1, (File)o2);
}
private int compare( File f1, File f2){
long result = f2.lastModified() - f1.lastModified();
if( result > 0 ){
return 1;
} else if( result < 0 ){
return -1;
} else {
return 0;
}
}
});
for(int i=0 ; i < 10 ; i++){
map.put(files[i].getName(), files[i].getPath());
}
request.setAttribute("map", map);
RequestDispatcher dispatcher = request.getRequestDispatcher("/welcome.jsp");
dispatcher.forward(request, response);
}
我 100% 确定错误出在这段代码中,因为当我对 map
、jsp 进行硬编码时会显示其中的数据。这有什么问题吗?大概是 path
??
编辑: 这是我的 jsp 代码:
<body>
<div id = "Header">
<h1>You Blogger</h1>
</div>
<div id = "data">
<c:forEach var="country" items="${map}">
${country.key} + ${country.value}
<br/>
</c:forEach>
<form action="new_post" method = "POST">
<input type = "submit" value = "Add A New Post" ></input>
</form>
</div>
</body>
The project is running on apache tomcat 8.0 and I'm using eclipse Luna for developing.
我认为问题出在您代码的第 2 行和第 3 行:
String path = "localhost:8080/YouBlogger/Posts/";
File dir = new File(path);
这将做的是查看目录 c:/localhost:8080/YouBlogger/Posts/
并列出那里的文件,因为这个目录可能不存在于您的驱动器 c: 上,所以它 return 什么也不会。如果你想列出远程机器上的文件,你需要使用一些 HTTP 客户端或在那里放置现有目录的路径。
File
构造函数需要一个真实的路径来访问本地目录。所以你不应该使用 localhost:8080
访问它,而应该使用下面的真实路径访问目录
String path="/home/test/apache/webapp/projectname/YouBlogger/Posts/"
如果您不想对路径进行硬编码,您可以使用 request.getSession().getServletContext().getRealPath("/")
获取网络服务器目录的真实路径