0

Scriptlet variable in div id

I have the same question as in the above link but with Struts2, when I put the code within as below it does not work.

 <s:form>

        <%!int i, j;%>
        <%
            for (i = 0; i < 5; i++) {
        %>
        <%
            for (j = 0; j < 5; j++) {
        %>
        <div class="One" id="j<%=j%>">
            Hey<%=i%></div>
        <%
            }
        %>
        <%
            }
        %>
    </s:form>

I know this is not good to use scriptlet, but atleast it should work.

Community
  • 1
  • 1
Amit Kumar
  • 2,685
  • 2
  • 37
  • 72
  • what do you mean by `does not work` ? What html output do you get for the above code ? – coding_idiot Sep 25 '13 at 21:00
  • I want id to be the value of "i". But in html it shows id="j<%=j%>" i.e. the jsp code is not evaluated. But it is evaluated when I don't use struts tags. – Amit Kumar Sep 26 '13 at 05:11

1 Answers1

1
  1. Avoid scriptlets
  2. Your code will generate multiple element with the same ID, that is not allowed.

Solution:

  1. Use Struts Iterator
  2. Place both variables in ID.


<s:form>
    <s:iterator begin="0" end="5" status="i" >
        <s:iterator begin="0" end="5" status="j" >
            <div id="<s:property value="%{'i' + #i.index + 'j' + #j.index}"> ">
                Hey <s:property value="%{#i.index}"/> 
            </div>
        </s:iterator>
    </s:iterator>
</s:form>

Note that

The begin, end and step attributes are only available from 2.1.7 on

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243