0
<% 
    RegisterAction aro=new RegisterAction();    
    int count=aro.getLi().size();
%>

<s:iterator value="li" begin="0" end="1">
    <fieldset>
        name     : <s:property value="name"     /><br/>
        password : <s:property value="password" /><br/>
        email    : <s:property value="email"    /><br/>
        gender   : <s:property value="gender"   /><br/>
        country  : <s:property value="country"  /><br/>
    </fieldset>
</s:iterator>

How to set end attribute value dynamically for iteration, reading the count variable ?

If I use end="<%=count%>" this is not working.

If I use end="count" it's working but I am getting same result multiple of numbers if I refresh the page or reload.

rtruszk
  • 3,902
  • 13
  • 36
  • 53
santosh
  • 21
  • 8

2 Answers2

0

You can use the #attr notation in OGNL to read variables set within Scritplet blocks, if you push them in the pageContext:

<% 
    RegisterAction aro = new RegisterAction();
    int count = aro.getLi().size();
    pageContext.setAttribute("count", count); // pushing the variable into the pageContext
%>

<s:iterator value="li" begin="0" end="%{#attr['count']}">
    <fieldset>
        name     : <s:property value="name"     /><br/>
        password : <s:property value="password" /><br/>
        email    : <s:property value="email"    /><br/>
        gender   : <s:property value="gender"   /><br/>
        country  : <s:property value="country"  /><br/>
    </fieldset>
</s:iterator>

But you should never use Scriptlet for many reasons.

It's also very easy to use Struts tags instead of scriptlets for your purposes, like shown in this answer.


EDIT:

If I use end="count" it's working but I am getting same result multiple of numbers if I refresh the page or reload.

It's however unclear why you are instantiating an Action in a Scriptlet block, and then why you do expect the result of count to be different across the page loadings, since your code is always referring to a collection in its initial state.

If RegisterAction is your current action, then what you want is probably:

<s:iterator value="li" begin="0" end="%{li.size()}">

that is in fact

<s:iterator value="li">

and you are overcomplicating a simple iteration by factor 2337.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • i tried this also but same how previously its keep on loading multiple number of same record after refresh and re-load like only working. – santosh Jul 27 '15 at 12:50
  • ya may be yours answer correct, ya i already tested above two answer but i expected some accurate answer over that prob. may be as u told collection keep on iteration from initial state. – santosh Jul 27 '15 at 13:08
  • @santosh, you expect an accurate answer to a completely unclear question ? ***What*** you want to achieve ***exactly*** ? Because (now) it seems you just need to iterate a collection. And if it is like that, the whole scriptlet question is irrelevant and misleading, even though if well answered. Just use iterator tag, witho no start nor end. – Andrea Ligios Jul 27 '15 at 13:33
  • @ Andrea Ligios, ya i tried how u told, actually i m reading the data from db and set to list and printing, how many records there in db that much record only to display in jsp even afer refresh also, but here same record keep on displaying multiple number of time as how we refresh the page, and if we use integer value within end attribute that will work properly, then what is the use of that...? so i am thinking. – santosh Jul 27 '15 at 13:40
0

hi lastly i got answers as how i want you can use this below code in action class for initialize arraylist from db count

int l1=st.executeUpdate("select count(*) from strutsuser");
         li=new ArrayList<RegisterAction>(l1);

view jsp is

<s:iterator value="li" >

<fieldset >
name : <s:property value="name"/><br/>
password : <s:property value="password"/><br/>
email : <s:property value="email"/><br/>
gender : <s:property value="gender"/><br/>
country : <s:property value="country"/><br/>

</fieldset>

</s:iterator>

this will solve the number of multiple display of same record even if we refresh the page with struts iterator.

santosh
  • 21
  • 8