public class PagingAction extends org.apache.struts.action.Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//-------------create session--------------
HttpSession sess = request.getSession();
//-------success and error forwards-------------
String SUCCESS = "success";
String ERROR = "error";
ActionMessages errors = new ActionMessages();//----all errors----------
//----Utility Class containing the createPartedList() function--------
UtilityClass uc = new UtilityClass();
//-----------------start of trycatch-----------------------
try{
String list ="";
int rec = 5; //no of rec per page
int total=0;
int pages=0;
int page = Integer.parseInt(request.getParameter("pg"));
switch (mod){
/*depending on different criteria you can set the incoming List object
and other values like forward path.*/
case 1: list = "list1";
//case 2: list1 = "list2";
}
List data = (ArrayList)sess.getAttribute(list);
if(data==null||data.size()<1){
errors.add("error.errmsg",new ActionMessage("error.noitem"));
}
total = data.size(); //total item in collection
pages=(total%rec==0)?total/rec:(total/rec)+1;
//total no. of pages set as session attribute
sess.setAttribute("pages", pages);
//return the list corresponding to the particular page number
data = (ArrayList)uc.createPartedView(data, rec, page)*;
//refer to previous post for this function
if(data.size()<1){
errors.add("errnoitem",new ActionMessage("error.noitem"));
}
//setting the list as request attribute
request.setAttribute(list, data);
}catch(Exception e){
errors.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("error.errmsg"));
}//----------------end of trycatch---------------
saveErrors(request,errors);//---save all error------
//Forward the page
return mapping.findForward(SUCCESS);
}
}
also you can create a jsp include page which will retrieve the total no of pages
and display the page numbers as hyperlink calling to this paging action class
this PagingAction class can also be created as Filter
