how to  pass dynamic offset value to struts logic:iterate tag in a jsp page

Arthur Wang
Arthur Wang used Ask the Experts™
on
I have an application using struts, oracle, and jsp, I want to display result from database in pages by using offset and length in logic tag.
in my action class:

    int offset = 31;//for test purpose, its actual value will be 1, 31, 61, 91.......
    System.out.println("offset is : "+offset);
    pageBean.setOffSet(offset);

in pageBean class:
  public void setOffSet( int offSet)
  {
        this.offSet = offSet;
  }
  public int getOffSet()
  {
        return this.offSet;
  }

then I put the offset value in jsp:

<logic:iterate id="dateBean" indexId="i" name="dateBeanList" offset='<bean:write name="pageBean" property="offSet"/>'  length="30" >
<tr>
................
</tr>
</logic:iterate>

but the value of offset will never come to the jsp page, how come this happen?

I tried to fix this problem by transfer value of offSet in request attribute like:

    request.setAttribute("offset", Integer.valueOf(String.valueOf(offset)));

and in jsp use the following way to get it:

<logic:iterate id="dateBean" indexId="i" name="dateBeanList" offset='<%= request.getAttribute("offSet") %>'  length="30" >
<tr>
................
</tr>
</logic:iterate>

but it still does not work. on the same page, I print out the value on the webpage by using:

<tr><td colspan = 12>OffSet is: <bean:write name="pageBean" property="offSet"  /></td></tr>

it can show the correct value I need. but just don't know why it does not work with logic:iterate tag.

Can anybody help ? a piece of code will be greatly appreciated



Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
>>> I tried to fix this problem by transfer value of offSet in request attribute like:

    request.setAttribute("offset", Integer.valueOf(String.valueOf(offset)));

and in jsp use the following way to get it:

<logic:iterate id="dateBean" indexId="i" name="dateBeanList" offset='<%= request.getAttribute("offSet") %>'  length="30" >

i noticed one thing in the above .
 request.setAttribute("offset", Integer.valueOf(String.valueOf(offset))); -----offset --s (small ,not caps)
 offset='<%= request.getAttribute("offSet") %>'  ----- offSet -S(caps).

 try with offset='<%= request.getAttribute("offset") %>'

Author

Commented:
Thanks for the post from Vsubram2, I think it's a typo when I copy my code on this board, after I correct the upper case, lower case problem: make all of them lower case: "offset" in action class and "offset"  in jsp, I still got the following error:

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 59 in the jsp file: /WEB-INF/forms/sales/viewJobList.jsp
Generated servlet error:
The method setOffset(String) in the type IterateTag is not applicable for the arguments (Object)


      org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
      org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
      org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:397)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
      org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:296)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
      org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1056)
      org.apache.struts.tiles.TilesRequestProcessor.doForward(TilesRequestProcessor.java:261)
      org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:388)
      org.apache.struts.tiles.TilesRequestProcessor.processForwardConfig(TilesRequestProcessor.java:316)
      org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:231)
      org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
      org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
      org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1056)
      org.apache.struts.tiles.TilesRequestProcessor.doForward(TilesRequestProcessor.java:261)
      org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:388)
      org.apache.struts.tiles.TilesRequestProcessor.processForwardConfig(TilesRequestProcessor.java:316)
      org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:231)
      org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
      org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
      org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:75)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.8 logs.

then I change the code in action class into:

request.setAttribute("offset", String.valueOf(offset));

instead of

request.setAttribute("offSet", Integer.valueOf(String.valueOf(offset)));

I got the same error message, where is the problem and what should I do next?

thanks for any help
Commented:
try
request.setAttribute("offset", String.valueOf(offset));
and
'<%= (String)request.getAttribute("offset") %>'

Commented:
please check this example code ,which may help u more.

    <%request.setAttribute("offset","1");%>
    <logic:iterate name="serverTypes" id="serverType" offset='<%=(String)request.getAttribute("offset")%>' >
     <bean:write name="serverType"/>
     </logic:iterate>

Commented:
The solution by vsubram2 would work.  I wrote my code using vsubram2's tip:
I'm trying to implement a paging logic on a personList containg personVO objects so:

1. I got personList object earlier in my code.
2. Then got total records of personList as shown.
3. Then got start record number using the pageNum I got from request parameter.
4. Then iterated desired record numbers using the offset & length of the iterate tag.

Worked out pretty well for me, here is the code:
<bean:size id="totalRecords" name="personList"/>
<% int recordCount = totalRecords.intValue();
   int totalPages =  (recordCount%6==0?recordCount/6:recordCount/6+1);
   int curStartRecord = (Integer.parseInt(pageNum)-1)*6; %>

<logic:present name="personList">
<logic:iterate id="cvv" name="personList" offset="<%=String.valueOf(curStartRecord)%>" scope="page" length="6" >
<tr>
<td><bean:write name="cvv" property="firstName"/></td>
<td><bean:write name="cvv" property="middleName"/></td>
<td><bean:write name="cvv" property="lastName"/></td>
<td><bean:write name="cvv" property="reasonDescription"/></td>
</tr>
</logic:iterate>
</table>
</logic:present>

Hope this helps somebody either an example of iterate tag using offset attribute, or for paging logic.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial