how to do paging in Struts logic:iterate
i want to put next and previous links on my jsp and if user clicks on next it will give the next record., so as with the previous.
i have written a code in Action file as
public void getGridData(HttpServletRequest req)
{
String offset_val="0";
String offset_length="5";
try{
try
{
String pg=(String)req.getSession().getAttribute("pageNumber");
System.out.println("VALUE parameter"+pg);
if(pg==null)
{
System.out.println("not null or 0");
req.getSession().setAttribute("paging","paging");
req.getSession().setAttribute("offset_val",offset_val);
req.getSession().setAttribute("offset_length",offset_length);
}
else
{
System.out.println("else");
int ii=Integer.parseInt((String)req.getSession().getAttribute("pageNumber"));
//GET VALUE FOR OFFSET
int offset_val_ii=Integer.parseInt((String)req.getSession().getAttribute("offset_val"));
int offset_length_ii=Integer.parseInt((String)req.getSession().getAttribute("offset_length"));
//offset_val_ii=Integer.parseInt(offset_val)*2;
offset_val_ii=offset_length_ii;
//offset_length_ii=Integer.parseInt(offset_length)+4;
offset_length_ii=offset_val_ii+5;
offset_val=""+offset_val_ii;
offset_length=""+offset_length_ii;
req.getSession().setAttribute("paging","paging");
req.getSession().setAttribute("offset_val",offset_val);
req.getSession().setAttribute("offset_length",offset_length);
}
}catch(Exception exc)
{
System.out.println("ERRRRORRRR");
exc.printStackTrace();
}
String s =" from Aircraft_Master Aircraft_Master";
Query query = getSession().createQuery(s);
Iterator iterator = query.iterate();
aircraftGrid.clear();
while(iterator.hasNext())
{
Aircraft_Master userrecord = (Aircraft_Master)iterator.next();
String print_active_deactive=userrecord.getEnable();
if(print_active_deactive.equalsIgnoreCase("1"))
{
print_active_deactive="Active" ;
}
else
{
print_active_deactive="Deactive" ;
}
aircraftGrid.add(new Aircraft_Master(userrecord.getModel(),userrecord.getYearOfMake(),userrecord.getNotes(),userrecord.getLastPosition(),userrecord.getRegistrationNo(),userrecord.getLastMaintenanceDate(),print_active_deactive));
}
session.flush();
}
catch(Exception e){
e.printStackTrace();
System.out.println(e.getMessage());}
}
My jsp file is Read as
<%@ taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/tlds/struts-logic.tld" prefix="logic" %>
<% if(session.getAttribute("user")==null){%>
<jsp:forward page="index.jsp"/>
<%}%>
<html:html>
<jsp:include flush="true" page="WEB-INF/global/header.jsp"/>
<html:errors/>
<!-- content starts -->
<table width="970" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="15" height="12">
<img src="common/images/cantre_table_01.jpg" alt="top_left" width="15" height="12" /></td>
<td height="12" background="common/images/cantre_table_02.jpg" ></td>
<td width="14" height="12"><img src="common/images/cantre_table_03.jpg" alt="top_right" width="14" height="12" /></td>
</tr>
<tr>
<td background="common/images/cantre_table_04.jpg" width="15" ></td>
<td valign="top">
<%
String pageNumber="0";
if(request.getSession().getAttribute("pageNumber")!=null)
{
pageNumber=new String((String)request.getSession().getAttribute("pageNumber"));
}
String offset_val="";
String offset_length="";
if(request.getSession().getAttribute("paging")!=null)
{
offset_val=new String((String)request.getSession().getAttribute("offset_val"));
offset_length=new String((String)request.getSession().getAttribute("offset_length"));
System.out.println(" GET JSP offset_val : "+offset_val);
System.out.println(" GET JSPoffset_length : "+offset_length);
}
%>
<table border="0" cellpadding="0" cellspacing="0" bordercolor="#00FF00">
<tr>
<td align="right" colspan="7"><html:link forward="aircraftdetails"><img src="common/images/new_on.gif" alt="New" border="0" /></html:link></td>
</tr>
<logic:present name="aircraftGrid">
<tr class="headerColoum">
<td align="left" >Reg. No</td>
<td align="left" >Model</td>
<td align="left" >Year</td>
<td align="left" >Notes</td>
<td align="left" >Last Position</td>
<td align="left" >Last Mainteneance Date</td>
<td align="left" >Active/Deactive</td>
</tr>
<%--
<logic:iterate id="grid" name="aircraftGrid" scope="request" offset="1" length="2"> --%>
<logic:iterate id="grid" name="aircraftGrid" scope="request" offset="<%=offset_val%>" length="5">
<tr>
<td align="left" class="read_Hyper">
<a href="AircraftGrid.cargo?regno=<bean:write name="grid" property="registrationNo"/> ">
<bean:write name="grid" property="registrationNo"/>
</a>
</td>
<td align="left" class="read"><bean:write name="grid" property="model"/></td>
<td align="left" class="read"><bean:write name="grid" property="yearOfMake"/></td>
<td align="left" class="read"><bean:write name="grid" property="notes"/></td>
<td align="left" class="read"><bean:write name="grid" property="lastPosition"/></td>
<td align="left" class="read"><bean:write name="grid" property="lastMaintenanceDate"/></td>
<td align="left" class="read"><bean:write name="grid" property="enable"/></td>
</tr>
</logic:iterate>
<%
try
{
int pageNumber_int=Integer.parseInt(pageNumber);
++pageNumber_int;
pageNumber=""+pageNumber_int;
System.out.println(" set pageNUMBER : "+pageNumber);
request.getSession().setAttribute("pageNumber",pageNumber);
//SET VALUE =4
//request.getSession().setAttribute("offset_val",pageNumber);
//request.getSession().setAttribute("pageNumber",pageNumber);
System.out.println("set parameter");
}catch(Exception aa)
{
System.out.println("aircraft.jsp error");
aa.printStackTrace();
}
%>
<tr><td><html:link action="/AircraftGrid" >Next</html:link></td></tr>
</logic:present>
<logic:notPresent name="aircraftGrid">
<tr>
<td colspan="5" align="center" class="read">No Record....</td>
</tr>
</logic:notPresent>
</table>
</td>
<td background="common/images/cantre_table_06.jpg" width="14"></td>
</tr>
<tr>
<td width="15" height="17"><img src="common/images/cantre_table_07.jpg" alt="bottom_left" width="15" height="17" /></td>
<td background="common/images/cantre_table_08.jpg"></td>
<td width="14" height="17"><img src="common/images/cantre_table_09.jpg" alt="bottom_right" width="14" height="17" /></td>
</tr>
</table>
<!-- content ends-->
<jsp:include flush="true" page="WEB-INF/global/footer.jsp"/>
</html:html>

