1

I am in process to migrate some Struts1 code to Struts2 and confused about migration of below tags.

Struts1:

<logic: notEmpty name="newsListForm" property="generalNews.list">
    <logic:iterate id="generalNews"
        name="newsListForm"
        property="generalNews.list"
        type="com.ab.NewsDTO"
        indexId="rowCount">
        <%
            if(rowCount.longValue() % 2 == 0){
             bgcolor = "#FFFFF";
            }else{
             bgcolor = "#EEEEE";
            }
        %>
     </logic: iterate>
    </logic: notEmpty>

I have migrated above code as:

Struts2

<s:if test="(newsListBean.generalNews.list != null)">
 <s:iterator id="generalNews"
            values="generalNews.list"
            status="incrementer">
    <s:if test="#incrementer.even==true">
<% pageContext.setAttribute("bgcolor","#FFFFFF")%>
</s:if>
 <s:else>
<% pageContext.setAttribute("bgcolor","#EEEEEE")%>
</s:else>
>
 
</s:iterator>
</s:if>

Is it a correct way to migrate these iterate & <logic:notEmpty> tags?

2 Answers 2

0

No, it's not correct. The original code use tags and scriptlet code to generate some text which is probably a JavaScript code.

But converted code doesn't do it. Instead it is used to set a page context attribute.

9
  • can you tell me the equivalent Struts2 code or some guidelines how it's done?
    – TechyGuy
    Feb 6, 2018 at 18:38
  • Thanks! What happens to DTO object given in "type" in S1 code. Don't we need it in S2?
    – TechyGuy
    Feb 7, 2018 at 6:48
  • @AbN It's not clear what you're asking; the type of the object is irrelevant. Feb 7, 2018 at 14:17
  • @DaveNewton I meant "type" attribute of the <logic:iterate> tag from Struts1. I am aware that S2 <iterator> tag does not have type attribute but what happens to DTO class then?
    – TechyGuy
    Feb 7, 2018 at 16:05
  • @AbN What "happens" to it? Nothing. OGNL, like standard JSP EL, relies on JavaBean naming conventions. Type is irrelevant. Feb 7, 2018 at 16:07
0

Somewhat off-topic, but related: there's a way, way cleaner way to do this by using proper CSS.

First have classes for the row colors: this is something you should have anyway rather than wedging presentation definitions into JSP code:

<style>
  .row-0 { bgcolor: #ffffff }
  .row-1 { bgcolor: #eeeeee }
</style>

Then use those to construct classnames for whatever it is you need (here, likely <td>s):

<s:iterator id="generalNews" values="generalNews.list" status="stat">
  <tr class="row-${stat.count % 2}">
    <!-- ... Table row content ... -->
  </tr>
</s:iterator>

(Roughly; this is from memory.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.