c:forEachæ æ³æ¾ç¤ºä¿¡æ¯çå¯è½åå 以åéè¦æ³¨æçå°æ¹
æç« ç®å½
1.注æçå°æ¹
1.第ä¸ç¹
è®°å¾å¨JSP页é¢å ä¸
<%@ taglib uri=âhttp://java.sun.com/jsp/jstl/coreâ prefix=âcâ %>
è¿å¥è¯å¾éè¦ï¼å¦ææ²¡æï¼é£ä¹JSP页é¢å°±æ æ³æ¾ç¤ºåºä¿¡æ¯
è®°å¾å¯¼å
¥å
2.第äºç¹
æ£ç¡®çä½¿ç¨æ°ç»
æ°ç»çé¿åº¦æ¯ä¸å®çï¼å¦æè¦ä¼ å
¥æªç¥é¿åº¦çæ°æ®ï¼è®°å¾ä½¿ç¨Listï¼ä½¿ç¨Listè¦è®°å¾åå§åï¼å³
List < String> a = new ArrayList(); ä¸è½å纯çåList< String> aï¼ï¼æ·»å æ°ç»å
ç´ çæ¶åè¦ä½¿ç¨addæ¹æ³
2.ä¾å
Servlet
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ShowFiles
*/
@WebServlet("/ShowFiles")
public class ShowFiles extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ShowFiles() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
List<String> a = new ArrayList();
String[] test={"a","b","c"};
int j=0;String b;
String path = "G:\\rsdownload"; // è·¯å¾
File f = new File(path);
if (!f.exists()) {
System.out.println(path + " not exists");
return;
}
File fa[] = f.listFiles();
for (int i = 0; i < fa.length; i++) {
File fs = fa[i];
if (fs.isDirectory()) {
//System.out.println(fs.getName()+" "+i);
b=fs.getName();
a.add(b);
} else {
//System.out.println(fs.getName());
b=fs.getName();
a.add(b);
}
}
request.setAttribute("File", a);
request.setAttribute("test", test);
System.out.println(a);
request.getRequestDispatcher("listFiles.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
JSP
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<!--<%request.getAttribute("File"); %> -->
<table class="table">
<tr class="table_header">
<td>
File Name
</td>
<td>
Downlink
</td>
</tr>
<c:forEach items="${File }" var="File" >
<td>
${File}<br>
</td>
<td>
www.google.com
</td>
<tr>
</c:forEach>
</table>
</body>
</html>
è¿è¡ç»æ
对äºä»Servletå°JSPç䏿乱ç é®é¢ï¼æä»¬è¦å¨doGetæ¹æ³ç第ä¸å¥å ä¸response.setCharacterEncoding(âutf-8â);è¿éæ¯è¦ä¹æresponse.getWriter().append("Served at: ").append(request.getContextPath());è¿å¥è¯å æï¼è¦ä¹å°±æresponse.setCharacterEncoding(âutf-8â);åå¨å®çåé¢ï¼èä¸è¦åå¨å®çåé¢å°±è¡äºã
3.åè
https://blog.****.net/qq_34646449/article/details/76146659
https://zhidao.baidu.com/question/394152982.html
https://zhidao.baidu.com/question/1447371832991262740.html
https://blog.****.net/eminem2011/article/details/7065860
http://ask.zol.com.cn/x/5321764.html
https://blog.****.net/x763795151/article/details/52472014