简体   繁体   English

Jstl 迭代列表

[英]Jstl iterate over a List

I am trying to iterate over a list and trying to print the value as:我正在尝试遍历列表并尝试将值打印为:

<jsp:useBean class="com.lftechnology.db.EmployeeDaoImpl" id="empImpl"></jsp:useBean>
<jsp:useBean class="com.lftechnology.employee.Employee" id="employee"></jsp:useBean>

<%
List<com.lftechnology.employee.Employee> empList = null; 
empList = empImpl.getAllEmployee();
out.println(empList.size());
%>


<c:forEach items="${empList}" var="element"> 
  <tr>
    <td>${element.name}</td>
    <td><c:out value="${element.name}" /></td>
     </tr>
</c:forEach>

However, only the size of the list is printed not the name as defined inside jstl.但是,只打印列表的大小,而不是在 jstl 中定义的name Any help?有什么帮助吗? I want to print the all the attributes of employee object.我想打印employee对象的所有属性。

Why do you use the scriptlet at all?为什么要使用scriptlet? Something like this should work (I could not test it):这样的事情应该可以工作(我无法测试):

<jsp:useBean class="com.lftechnology.db.EmployeeDaoImpl" id="empImpl"></jsp:useBean>

<c:forEach items="${empImpl.allEmployee}" var="element"> 
  <tr>
    <td>${element.name}</td>
    <td><c:out value="${element.name}" /></td>
  </tr>
</c:forEach>

Some background一些背景

Just declaring a variable in a scriptlet does not automatically make it available to the JSTL runtime.仅在 scriptlet 中声明变量不会自动使其可用于 JSTL 运行时。 In fact JSTL works on the pageContext injected into each JSP at runtime by the container.事实上,JSTL 是在运行时由容器注入到每个 JSP 中的pageContext上工作的。 So if you really need to declare a variable in a scriptlet and want to make it available to JSTL you have to do this explicitly:因此,如果您确实需要在 scriptlet 中声明一个变量并想让它对 JSTL 可用,则必须明确地执行以下操作:

<%
List<String> stuff = new ArrayList<String>();
request.setAttribute("mystuff", stuff);
%>

<c:out value="${mystuff" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM