-->

Accessing value from bean:write in value attribute

2019-08-30 11:00发布

问题:

I'm developing an application using Struts 1.3.10

I need to iterate 2 lists in order to print the result in jsp. The first list iteration needs to be used to select elements from list 2. For that reason I'm trying to do like this:

 <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>

But, when I call "bean:write" inside value of "logic:equal" I get an error. Do you know how to solve it?

As you sugest me I have used JSTL tags to get the solution, but in the source code of the web page I have this result:

    <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>

I seems that is not reading the bean information... Any idea?

回答1:

Learn the JSTL and the JSP EL, and use it instead of these obsolete struts tags:

<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, the JSTL exists for something like 10 years. And since then, the Struts documentation says:

Note: - Many of the features in this taglib are also available in the JavaServer Pages Standard Tag Library (JSTL). The Apache Struts group encourages the use of the standard tags over the Struts specific tags when possible.