【问题标题】:Accessing value from bean:write in value attribute of logic:equal Struts从bean访问值:写入逻辑的值属性:equal Struts
【发布时间】:2013-02-01 14:19:12
【问题描述】:

我正在使用 Struts 1.3.10 开发应用程序

我需要迭代 2 个列表才能在 jsp 中打印结果。第一次列表迭代需要用于从列表 2 中选择元素。因此,我尝试这样做:

 <logic:iterate name="bodyForm" property="domainList" id="domList">
     <div><h1><bean:write name="domList" property="domain"/><h1>
          <ul> <logic:iterate name="bodyForm" property="locationsList" id="locList" >
                   <logic:equal name="locList" property="domain" value="<bean:write name="domList" property="domain"/>" >
                       <li><div>....</div></li>
                   <logic:equal>
               </logic:iterate>
           </ul>
     </div>
</logic:iterate>

但是,当我在“logic:equal”的值内调用“bean:write”时,我得到一个错误。 你知道怎么解决吗?

如你所言,我使用 JSTL 标签来获得解决方案,但在网页的源代码中我有这个结果:

    <h1>domList.domain</h1>
        <ul>
          <li class="grey">
             <div>locList.countries.name </div>
             <div>locList.name</div>
             <div>locList.hostname</div>
             <div>locList.ip</div>
          </li>
          <li class="">
             <div>locList.countries.name </div>
             <div>locList.name</div>
             <div>locList.hostname</div>
             <div>locList.ip</div>
          </li>
        </ul>

我似乎没有读取 bean 信息...有什么想法吗?

【问题讨论】:

  • 你还没有显示你的 JSP 的代码。
  • 我找到了将 value="${domList.domain}" 放在 jsp 文件的符号中的解决方案。
  • 我现在的问题是在同一个 bean 中迭代 2 次。我的意思是,尝试将相同的 foreach 迭代与相同的“项目”值放在一起,以获得具有不同信息的两列。有可能吗?
  • 这听起来像是一种低效的做事方式,但它当然是可能的。 forEach 循环就像 Java 中的 for 循环。嵌套任何你想要的数字。

标签: jsp struts logic javabeans


【解决方案1】:

学习 JSTL 和 JSP EL,并使用它来代替这些过时的 struts 标签:

<c:forEach var="domList" items="${bodyForm.domainList}">
    <div>
        <h1><c:out value="${domList.domain}"/><h1>
        <ul> 
            <c:forEach var="locList" items="${bodyForm.locationsList}">
                <c:if test="${locList.domain == domList.domain}">
                    <li><div>....</div></li>
                </c:if>
           </c:forEach>
       </ul>
    </div>
</c:forEach>

AFAIR,JSTL 存在大约 10 年。从那时起,the Struts documentation 说:

注意: - 此标记库中的许多功能在 JavaServer Pages 标准标记库 (JSTL) 中也可用。 Apache Struts 小组鼓励尽可能使用标准标签而不是 Struts 特定标签。

【讨论】:

  • 感谢您的快速回复。我已经使用了这些标签,但没有打印任何东西......
  • 然后调试。生成的 HTML 是什么样的?您是否添加了 JSTL 核心 taglib 声明?你是否在你的 webapp 中包含了 JSTL jar 文件?阅读stackoverflow.com/tags/jstl/info
  • 我添加了 de JSTL jar 文件并添加了 tadlib,但屏幕中的结果始终是我可能缺少的 var 名称(如 domList.domain 或 locList.name)?
  • 不要看浏览器中显示的页面。查看生成的 HTML 源代码(在浏览器中使用“查看页面源代码”)。通过编辑您的问题,告诉我们它的样子,并向我们展示您的 JSP 的源代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-17
  • 2012-06-19
  • 2011-06-03
相关资源
最近更新 更多