13-JSTL详解

一、抛出问题

1.1 先看这么一段代码

  • UserServlet.java

    package com.sunshine.servlet;
    
    import com.sunshine.entity.User;
    
    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.util.ArrayList;
    import java.util.List;
    
    /**
     * @author 田港
     * @version 1.0
     * @date 2021-04-02 23:42
     */
    @WebServlet("/user")
    public class UserServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            List<User> userList = new ArrayList<>();
            userList.add(new User(1, "张三", 78.6));
            userList.add(new User(2, "李四", 79.6));
            userList.add(new User(3, "王五", 80.2));
            req.setAttribute("userList", userList);
            req.getRequestDispatcher("index.jsp").forward(req, resp);
        }
    }
    
  • index.jsp

    <%--
      Created by IntelliJ IDEA.
      User: Administrator
      Date: 2021-03-31
      Time: 15:32
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>$Title$</title>
    </head>
    <body>
        <table>
          <tr>
            <th>编号</th>
            <th>姓名</th>
            <th>成绩</th>
          </tr>
          <tr>
            <td>${requestScope.userList[0].id}</td>
            <td>${requestScope.userList[0].name}</td>
            <td>${requestScope.userList[0].score}</td>
          </tr>
          <tr>
            <td>${requestScope.userList[1].id}</td>
            <td>${requestScope.userList[1].name}</td>
            <td>${requestScope.userList[1].score}</td>
          </tr>
          <tr>
            <td>${requestScope.userList[2].id}</td>
            <td>${requestScope.userList[2].name}</td>
            <td>${requestScope.userList[2].score}</td>
          </tr>
        </table>
    </body>
    </html>
    

    首先我们写了一段 Servlet 代码,将 user 数据写到集合中,然后传递给 jsp 文件。那么问题来了,前端如果不知道你的集合有几个对象,那么怎么搞

上一篇:EL和JSTL核心技术


下一篇:HTTP Status 500 - Handler processing failed; nested exception is java.lang.NoClassDefFoundError: jav