【问题标题】:Split array value in table JSP [duplicate]在表 JSP 中拆分数组值 [重复]
【发布时间】:2015-12-15 14:47:27
【问题描述】:

如何拆分 JSP 的数组值,我正在尝试拆分数组值并放入表中

 <%
        String query = request.getParameter("item");
    ItemSearch is = new ItemSearch();   
    ArrayList<Items> result = is.doSearch(query);
    for(Items item : result)
    {   
        out.println(item.getImg());
        out.println(item.getHref());
        out.println(item.getTitle());
        out.println(item.getPrice());
        out.println(item.getDesc());
        //out.println(item.toString());

    }

    %>

这是我的桌子。

<table border="1">
                <thead>
                    <tr>
                        <th>Description</th>
                        <th>Name</th>
                        <th>Price</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td><%= out.println(item.getDesc)%></td>
                        <td></td>
                        <td></td>
                    </tr>

任何人都可以帮助我进行此编码。我如何称呼每个项目

【问题讨论】:

  • 在打印语句中打印您的 html
  • 你写了你想要拆分值,你是在谈论循环你的 item 数组以连续显示每个项目吗?
  • @jhamon 是的,我在谈论循环以及如何在表格中显示

标签: java arrays jsp


【解决方案1】:

你可以这样做:

<table border="1">
<thead>
    <tr>
        <th>Description</th>
        <th>Name</th>
        <th>Price</th>
    </tr>
</thead>
<tbody>
<%
String query = request.getParameter("item");
ItemSearch is = new ItemSearch();   
ArrayList<Items> result = is.doSearch(query);
for(Items item : result)
{   
    %>
    <tr>
    <td><%= item.getDesc) %></td>
    <td><%= item.getTitle() %></td>
    <td><%= item.getPrice() %></td>
   </tr>
   <%
}
%>
</table>

但是

使用 Scriplets 是恐龙技术。你不应该再使用它。以 EL 为例

<table border="1">
    <thead>
        <tr>
            <th>Description</th>
            <th>Name</th>
            <th>Price</th>
        </tr>
    </thead>
    <tbody>
<c:forEach var="item" items="${yourListAsBean}">
<tr>
  <td><c:out value="${item.desc}" /></td>
  <td><c:out value="${item.title}" /></td>
  <td><c:out value="${item.price}" /></td>
</tr>
</c:forEach>
</table>

【讨论】:

  • 谢谢。它工作,但我能问你,我应该把 ${yourListAsBean}
【解决方案2】:

尝试像这样使用foreach of jstl
添加这些taglib:

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

以及显示数据的代码

   <c:forEach begin="0" end="${fn:length(result) - 1}" var="idx">
    <tr>
      <th>Description</th><td><c:out value="${result[idx]}"/></td>
      <th>Name</th><td><c:out value="${result[idx]}"/></td>
      <th>Price</th><td><c:out value="${result[idx]}"/></td>
   </tr>
 </c:forEach>

【讨论】:

    猜你喜欢
    • 2017-06-28
    • 2018-07-10
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    相关资源
    最近更新 更多