编程那点事编程那点事

专注编程入门及提高
探究程序员职业规划之道!

JSTL 案例 — 遍历list集合

package com.sponge.servlet;
import java.io.IOException;
import java.util.List;
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 com.sponge.dao.UserDao;
import com.sponge.entity.User;
@WebServlet("/showServlet")
public class ShowServlet extends HttpServlet { // 显示全部数据
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AdminDao dao = new AdminDao();
        List userList = dao.getAll();
        // servlet中存放list集合
        req.setAttribute("userList", userList);
        req.getRequestDispatcher("index.jsp").forward(req, resp);
    }
}

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!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=utf-8">

<title>显示</title>

    <style type="text/css">

            table {

                border: 1px solid pink;

                margin: 0 auto;

            }

            td{

                width: 150px;

                border: 1px solid pink;

                text-align: center;

            }

    </style>

</head>

<body>

    <table>

        <tr>

            <td>编号</td>

            <td>帐号</td>

            <td>密码</td>

            <td>操作</td>

        </tr>

        <c:forEach items="${userList}" var="user">

            <tr>

                <td>${user.id }</td>

                <td>${user.username }</td>

                <td>${user.userpwd }</td>

            </tr>

        </c:forEach>

    </table>

</body>

</html>


未经允许不得转载: 技术文章 » Java编程 » JSTL 案例 — 遍历list集合