首页 > 解决方案 > JSTL 语句出现在生成的 HTML 中的错误位置和格式

问题描述

我正在尝试生成从 Java 应用程序调用的通用列表页面。在 myApp.java 中,我创建/分配了三个请求属性:

            List<String> columnTitles = myClass.getColumnTitles ();
            request.setAttribute ("columnTitles", columnTitles);
            request.setAttribute ("columnCount", columnTitles.size ());
            List<List<String>> MyList = myClass.getList ();
            request.setAttribute ("columns", MyList);

            RequestDispatcher rd = request.getRequestDispatcher ("myListPage.jsp");
            rd.forward (request, response);

在文件“myListPage.jsp”中,我有:

<%@ page import="java.util.List"%>

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

      <div class="myTableBody">
        <p>Column count = <%= request.getAttribute ("columnCount") %></p> <!-- temp -->
        <table class="myTableClass">
          <thead>
            <tr>
              <c:forEach items="${columnTitles}" var="columnName">
                <th>${columnName}</th>
              </c:forEach>                                
            </tr>
          </thead>

不幸的是,会生成如下内容:

      <div class="myTableBody">
        <p>Column count = 6</p> <!-- temp -->
        <c:foreach items="[id, name, type, url, telephone, mobile]" var="columnName">
                  </c:foreach><table class="myTableClass">
          <thead>
            <tr>
              <th></th>

            </tr>
          </thead>

<c:foreach ...></c:foreach> 循环超出了标签的范围,更不用说标签了。显示列数,但不显示列标题或列本身。

该项目在 Apache Tomcat 8.0.21 下运行。我已经加载了 JSTL 1.2。在项目的 web.xml 文件中,web-app 版本为“2.5”。

在我必须编写自己的脚本之前有什么帮助(这是不好的做法)?

标签: jspjstl

解决方案


推荐阅读