• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2220
  • Last Modified:

pagination using display tag

Hi,

I`m using pagination tag library for paging list which i render in table ,the thing is that i wanted a to render record in limit means in first load i wanted to retrieve 10 records in next click or if i click page  2 it should retrieve next 10 record from 11 and so on ...so it is not in case of my application it load all  record when i click next or page 2


package com.vaannila;
 
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
 
 
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
 
/**
 *
 * @author eswar@vaannila.com
 */
public class UserAction extends org.apache.struts.action.Action {
 
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
 
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
            UserForm userForm = (UserForm) form;
            ActorData actorData = new ActorData();
            
            
            System.out.println("PageNumber:::::"+request.getParameter("d-49216-p"));
            userForm.setActorList(actorData.loadData());
            System.out.println("ActorList:::::::"+userForm.getActorList());
            return mapping.findForward(SUCCESS);
    }
}
 
 
 
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vaannila;
 
import java.util.ArrayList;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.util.List;
import org.displaytag.properties.SortOrderEnum;
 
 
/**
 *
 * @author eswar@vaannila.com
 */
public class ActorData {
 
    private String tvShow;
    private String userName;
    private String emailId;
 
    
    //private int size=4;
 
    public ActorData()
    {
 
    }
 
    public ActorData(String tvShow, String userName, String emailId)
    {
        this.tvShow = tvShow;
        this.userName = userName;
        this.emailId = emailId;
    }
 
    public ArrayList loadData()
    {
        ArrayList userList = new ArrayList();
        userList.add(new ActorData("The Office","Michael Scott","michael.scott@dundermifflin.com"));
        userList.add(new ActorData("The Office","Dwight Schrute","dwight.schrute@dundermifflin.com"));
        userList.add(new ActorData("The Office","Jim Halpert","jim.halpert@dundermifflin.com"));
        userList.add(new ActorData("The Office","Pam Beesly","pam.beesly@dundermifflin.com"));
        userList.add(new ActorData("The Office","Andy Bernad","andy.bernad@dundermifflin.com"));
        userList.add(new ActorData("The Office","Angela Martin","angela.martin@dundermifflin.com"));
        userList.add(new ActorData("Friends","Rachel Green","rachel.green@friends.com"));
        userList.add(new ActorData("Friends","Monica Geller","monica.geller@friends.com"));
        userList.add(new ActorData("Friends","Phoebe Buffay","phoebe.buffay@friends.com"));
        userList.add(new ActorData("Friends","Joey Tribbiani","joey.tribbiani@friends.com"));
        userList.add(new ActorData("Friends","Chandler Bing","chandler.bing@friends.com"));
        userList.add(new ActorData("Friends","Ross Geller","ross.geller@friends.com"));
        return userList;
    }
    public ArrayList loadDataDB()throws Exception{
           ArrayList userList=new ArrayList();
           ResultSet rs =null;
           DBConnection dbcon=new DBConnection();
           PreparedStatement stmt=null;
           Connection con=null;
           try{
               System.out.println("##################Connection Successfull");
               rs=dbcon.executeQuery("SELECT SYSDATE FROM DUAL");
           }catch(Exception e){
                e.printStackTrace();
           }finally{
                rs.close();
                dbcon.closeConnection();
            }
        //userList.add();
        return userList;
    }
 
    /**
     * @return the tvShow
     */
    public String getTvShow() {
        return tvShow;
    }
 
    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }
 
    /**
     * @return the emailId
     */
    public String getEmailId() {
        return emailId;
    }
 
}
 
 
 
 
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
package com.vaannila;
 
import java.util.ArrayList;
 
/**
 *
 * @author eswar@vaannila.com
 */
public class UserForm extends org.apache.struts.action.ActionForm {
 
   private ArrayList actorList;
   private String page=null;
   private int size=5;
   //private String d-49216-p=null;
    public UserForm() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @return the actorList
     */
    public ArrayList getActorList() {
        return actorList;
    }
 
    /**
     * @param actorList the actorList to set
     */
    public void setActorList(ArrayList actorList) {
        this.actorList = actorList;
    }
 
    public String getPage() {
        return page;
    }
 
    public void setPage(String page) {
        this.page = page;
    }
 
    public int getSize() {
        return size;
    }
 
}
 
 
 
 
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
package com.vaannila;
 
import java.util.ArrayList;
 
/**
 *
 * @author eswar@vaannila.com
 */
public class UserForm extends org.apache.struts.action.ActionForm {
 
   private ArrayList actorList;
   private String page=null;
   private int size=5;
   //private String d-49216-p=null;
    public UserForm() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @return the actorList
     */
    public ArrayList getActorList() {
        return actorList;
    }
 
    /**
     * @param actorList the actorList to set
     */
    public void setActorList(ArrayList actorList) {
        this.actorList = actorList;
    }
 
    public String getPage() {
        return page;
    }
 
    public void setPage(String page) {
        this.page = page;
    }
 
    public int getSize() {
        return size;
    }
 
}
 
 
 
 
<%--
    Document   : user
    Created on : Jan 7, 2009, 8:23:48 AM
    Author     : eswar@vaannila.com
--%>
 
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://displaytag.sf.net" prefix="display" %>
<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>User Details</title>
        <link href="style.css" rel="stylesheet" type="text/css" />
        <!--<script>
 
            /*
             * Retrieves the query parameter with the given parameter name or empty
             * string if not defined
             *
             * @param parameter name of the parameter to retrieve
             */
            function queryParam(parameter) {
                var searchString = top.location.search;
                searchString = searchString.substring(1);// ommit the leading '?'
 
                var paramValue = '';
                var params = searchString.split('&');
                for (i=0; i<params.length;i++) {
                    var paramPair = params[i];
                    var eqlIndex = paramPair.indexOf('=');
                    var paramName = paramPair.substring(0,eqlIndex);
 
                    if (paramName == parameter) {
                        paramValue = unescape(paramPair.substring(eqlIndex+1));
                        return paramValue;
                    }
                }
                return paramValue;
            }
</script>-->
 
    </head>
    <body>
        <!--<html:form action="/userAction.do">
            <table>
                <tr>
                    
                    <td><html:text property="minSalary"></html:text></td>
                </tr>
                <tr>
                    <td colspan="2"><html:submit property="submit" /></td>
                </tr>
            </table>
        </html:form>-->
        <display:table id="data" name="sessionScope.UserForm.actorList" requestURI="/userAction.do" pagesize="4" partialList="true" size="sessionScope.UserForm.size" sort="page">
            <display:column property="tvShow" title="TV Show" sortable="true"   />
            <display:column property="userName" title="User Name" sortable="true"  />
            <display:column property="emailId" title="Email Id" sortable="true"  />
        </display:table>
 
    </body>
</html>

Open in new window

0
Adwait Chitaley
Asked:
Adwait Chitaley
1 Solution
 
itzyCommented:
Hi
I think I didn't understand your question.
Do you want to retrieve records 10 by 10 (if you click page 2: request to the server to search record 11 to 20) ?
or
Do you want to paginate a collection without having to make a request each time you click page n (you search all the records just once)?
0
 
Gibu GeorgeCTOCommented:
If you want to set 10 records per page use the attribute pagesize="10"


<display:table id="data" pagesize="10" name="sessionScope.UserForm.actorList" requestURI="/userAction.do" pagesize="4" partialList="true" size="sessionScope.UserForm.size" sort="page">
            <display:column property="tvShow" title="TV Show" sortable="true"   />
            <display:column property="userName" title="User Name" sortable="true"  />
            <display:column property="emailId" title="Email Id" sortable="true"  />
        </display:table>

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now