【问题标题】:How to display Bean properties from ArrayList of Bean objects?如何从 Bean 对象的 ArrayList 中显示 Bean 属性?
【发布时间】:2013-02-18 13:19:53
【问题描述】:

我正在使用 struts 1.3。在我的操作类中,我从 DB 访问数据并在 Bean 类对象(每行一个对象)中设置值。最后,我将对象添加到 ArrayList 对象中。 现在在我的 jsp 中,我需要显示这些数据(Bean 属性值)。 我使用 scriptlet 做到了这一点,它工作正常。但我只想使用标签(按照标准方式推荐)。 任何人都可以提供一些想法如何使用<logic:iterate><nested:iterate> 可以正常工作吗?

这是我的jsp代码:

<% AppForm fm; %>
   <% Iterator itr; int i=0;
    ArrayList al=(ArrayList)request.getAttribute("data");
    System.out.println("ArrayList size is..."+al.size());
    if(al!=null)
    {
        for(itr=al.iterator(); itr.hasNext();i++)
        {
           fm=(AppForm)itr.next();
         %>
         <tr  id=i  onclick="toggle(this)" bgcolor="pink">
          <td align="center">
           <%= fm.getRegid() %>
          </td>
          <td align="center">
           <%= fm.getEid() %>
          </td>
          <td align="center">
           <%= fm.getFname() %>
          </td>
          <td align="center">
           <%= fm.getLname() %>
          </td>
          <td align="center">
           <%= fm.getDesignation() %>
          </td>
          <td align="center">
           <%= fm.getEmail() %>
          </td>
          <td align="center">
           <%= fm.getContact() %>
          </td>
          <td align="center">
           <%= fm.getAddress() %>
          </td>
          <td align="center">
           <%= fm.getQualification() %>
          </td>
          <td align="center">
           <%= fm.getJdate() %>
          </td>
          <td align="center">
           <%= fm.getReqdate() %>
          </td>
          <td align="center">
           <%= fm.getIpaddress() %>
          </td>
          <td align="center">
           <input type="radio" name="<%= fm.getEid() %>" value="approved" onclick="this.parentNode.parentNode.bgColor='DarkOliveGreen'"; />
          </td>
          <td align="center">
           <input type="radio" name="<%= fm.getEid() %>" value="rejected" onclick="this.parentNode.parentNode.bgColor='Orchid'"; />
          </td>

    </td>
         </tr> 
         <%
        }
    }
   %>

【问题讨论】:

  • 推荐的方法是使用 JSTL (&lt;c:forEach&gt;) 而不是过时的 Struts 逻辑标签。阅读文档,然后尝试一下。

标签: jsp struts jstl


【解决方案1】:

与struts标签相同的版本

<logic:iterate id="item" name="data" indexId="idx">
  Row index: <bean:write name="idx"/>
  <bean:write name="item" property="eid"/>
  <bean:write name="item" property="fname"/>
</logic:iterate>

【讨论】:

    【解决方案2】:

    试试这个

    <c:forEach var="i" items ="${data}">
      <tr>
         <td>${i.eId}</td>
         <td>${i.lName}</td>
         ....................
      </tr>
    </c:forEach>
    

    【讨论】:

    • 您好戴夫先生,谢谢您的回复。但我希望使用 struts 提供的标签。我认为如果可以使用 struts 标签,那么就不需要使用 JSTL 标签。如果您有任何想法,请告诉我。
    • @ShaileshSaxena 正如 JB 所说,当 JSTL 功能与 Struts 1 标签重叠时,应该首选 JSTL,如 mentioned in the S1 tag documentation
    猜你喜欢
    • 2013-04-26
    • 1970-01-01
    • 2019-03-03
    • 2011-06-03
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    • 2019-04-06
    • 1970-01-01
    相关资源
    最近更新 更多