You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring MVC中JSP的<c:forEach>标签无法输出列表内容求助

解决Spring MVC JSP中列表对象属性无法输出的问题

核心问题定位

从你提供的页面源码能看到,<c:foreach>标签被直接输出到页面,说明服务器未解析该JSTL标签——JSTL标签是大小写敏感的,标准循环标签是<c:forEach>(forEach首字母E为大写),小写的<c:foreach>不会被服务器识别,导致循环逻辑完全没执行,自然无法输出对象属性。

额外检查项

  1. 实体类的Getter方法
    EL表达式${professore.nome}依赖实体类的getNome()方法,需确保ProfessoreDAO类为对应字段提供标准Getter:
public class ProfessoreDAO {
    private String nome;
    private String cognome;
    private String materia;

    // 必须提供以下Getter方法
    public String getNome() {
        return nome;
    }

    public String getCognome() {
        return cognome;
    }

    public String getMateria() {
        return materia;
    }

    // 构造方法、Setter等其他代码
}
  1. JSP页面引入JSTL核心库
    在JSP文件顶部必须添加标签库声明,否则无法识别<c:forEach>等JSTL标签:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

修正后的JSP代码

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!-- 页面其他内容 -->
<table>
    <c:forEach var="professore" items="${professori}"> 
        <tr>
            <td>${professore.nome}</td>
            <td><c:out value="${professore.cognome}" /></td>
            <td><c:out value="${professore.materia}" /></td>
        </tr>
    </c:forEach>
</table>

内容的提问来源于stack exchange,提问作者CodeRonin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 23:19:01