taglib遍历foreach循环list集合
第一部导入jstl.jar
第二步进行list传输:
package com.aaa.servlet; import com.aaa.dao.IUserDAO; import com.aaa.dao.impl.UserDAOImpl; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import java.util.Map; @WebServlet("/test") public class IsLoginServlets extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); IUserDAO userDAO = new UserDAOImpl(); List<Map<String, Object>> selectAllAdmin = userDAO.selectAllAdmin(); req.setAttribute("list",selectAllAdmin); req.getRequestDispatcher("/indexs.jsp").forward(req,resp); } }
第三步测试:
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2019/6/25 Time: 8:27 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <title>Title</title> </head> <body> <table> <c:forEach items="${list}" var="a"> <tr> <td>${a.id}</td> <td>${a.admin}</td> <td>${a.password}</td> <td>${a.mininame}</td> </tr> </c:forEach> </table> </body> </html>