【问题标题】:How to display values in table in jsp?如何在jsp中显示表格中的值?
【发布时间】:2012-12-07 08:03:12
【问题描述】:

我是 jsp 和 jstl 的新手,我正在编写此代码以使用 for 循环从数组中获取值。我正在获取输出,但我想将该输出放入表中。如何做到这一点任何人都可以帮助我?

for ( int i =0; i < timeSize ; i++)
       {
         out.println(resource[i]);
         out.println(itimespend[i]);
         out.println(icostspend[i]);
         totalcost += itimespend[i] * icostspend[i];          
        }

【问题讨论】:

    标签: java javascript jsp jstl


    【解决方案1】:

    客户端,jsp

    <table>
        <tr>
            <th>ID</th>
            <th>Theme</th>
        </tr>
        <c:forEach items="${themeList}" var="theme" varStatus="status">
            <tr>
                <td>${theme.id}</td>
                <td>${theme.theme}</td>
            </tr>
        </c:forEach>
    </table>
    

    不要忘记声明jstl taglib,并将jstl libs添加到classpath

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

    你需要从服务器端返回一个列表给你的jsp,像这样:

    req.setAttribute("themeList", somelist);
    

    但考虑使用一些 MVC 框架

    【讨论】:

      【解决方案2】:

      这并不难。解决方案可以通过这样的实现来实现:

      <table>
      <%
      for ( int i =0; i < timeSize ; i++)
      {
      out.println(resource[i]);
      out.println(itimespend[i]);
      out.println(icostspend[i]);
      totalcost += itimespend[i] * icostspend[i];     
      %>
      
      <tr>
      <td> <%=resource[i]%></td>
      <td><%=itimespend[i]%> </td>
      <td> <%=icostspend[i]%></td>
      </tr>
      
      <%       
      }
      %>
      
      </table>
      

      将表格标签放在外面,以免每次都循环它并在每次循环时创建一个不同的表格。这还没有经过测试,但我想你会从这个中得到基本的想法。

      【讨论】:

        猜你喜欢
        • 2014-05-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-06
        • 1970-01-01
        • 1970-01-01
        • 2019-12-29
        • 1970-01-01
        相关资源
        最近更新 更多