发布于 5年前

java servlet 如何传值给JSP,并遍历

resp.setCharacterEncoding("UTF-8");
req.setAttribute("list", list);
req.getRequestDispatcher("/index.jsp").forward(req,resp); 

如何将servlet中的list传到,并遍历到表里

<table border="1">
  <tr>
    <th>员工编号</th>
    <th>员工姓名</th>
    <th>部门名称</th>
    <th>签到时间</th>
  </tr>
</table>
<table border="1">
        <tr>
                <th>员工编号</th>
                <th>员工姓名</th>
                <th>部门名称</th>
                <th>签到时间</th>
        </tr>

        <c:forEach items="${list}" var="dataList">
                <tr>
                        <td>${dataList.pin}</td>
                        <td>${dataList.Name}</td>
                        <td>${dataList.defaultdeptid}</td>
                        <td>${dataList.checktime}</td>
                </tr>
        </c:forEach>
</table>

我在jsp页面遍历之后出错

重: Servlet.service() for servlet [indexervlet] in context with path [/zpyr] threw exception [An exception occurred processing JSP page /index.jsp at line 33

30:         <c:forEach items="${list}" var="dataList">
31:             <tr>
32:                 <td>${dataList.pin}</td>
33:                 <td>${dataList.Name}</td>
34:                 <td>${dataList.defaultdeptid}</td>
35:                 <td>${dataList.checktime}</td>
36:             </tr>

Stacktrace:] with root cause

问题分析

req.setAttribute("list", list); 传一个list过去。

req.getRequestDispatcher("/index.jsp").forward(req,resp); 跳转并把数据传到该 index.jsp 页面.

index.jsp页面用 c 标签获取遍历。list. 属性

<table border="1">
  <tr>
    <th>员工编号</th>
    <th>员工姓名</th>
    <th>部门名称</th>
    <th>签到时间</th>
  </tr>
<c:forEach item="${list}" var="list">
<tr>
                <td>
            ${list.bh}
        </td>
        <td>
            ${list.name}
        </td>
        <td>
            ${list.mc}
        </td>
                <td>
            ${list.time}
        </td>
</tr>
</c:forEach>

</table>
©2020 edoou.com   京ICP备16001874号-3