dcsimg
Show the result-set in groups
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   zugurt_zugurt
Posted On:   Wednesday, May 12, 2004 04:09 AM

Hi,
I am using Struts in my applications. I get an array of data driven from tha database and I show the result set in a jsp page by using logic:iterate tag. Now the page shows all the results. I want to show 20 records per page and I want to use next, prev links to navigate through the result sets groups.
Can you tell me how to do it?

Thanks...

Re: Show the result-set in groups

Posted By:   sheetal_bhandari  
Posted On:   Wednesday, May 12, 2004 09:02 AM

Hi
I have created a tag class for the same.
I am attaching the code

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Category;

public class ShowPagescrollsTag extends TagSupport {

static Category cat = Category.getInstance("vaa");

int length=-1; //No of rows per page
Integer total; //The size of the collection to be displayed
int pageIndex = 1; //The page on display currently
//int pageRange = 10; //No of pages displayed on the scroll bar
String paramName="";
String paramValue="";

boolean error = false;
String errorCommon = "ShowPageScrollsTag ERROR:";
String errorMessage;
String context = "";

public Integer getTotal()
{
return total;
}

public int getLength()
{
return length;
}

public String getParamName()
{
return paramName;
}

public String getParamValue()
{
return paramValue;
}

public void setTotal(Integer total)
{
this.total = total;
}

public void setLength(int length)
{
this.length = length;
}

public void setParamName(String paramName)
{
this.paramName = paramName;
}

public void setParamValue(String paramValue)
{
this.paramValue = paramValue;
}

public int doEndTag() throws JspException
{
if(!error)
{
return EVAL_PAGE;
}
else
{
throw new JspException(errorCommon + errorMessage);
}
}

/**
* Generates the pagination section.
* @param Integer
* @param Integer total - representing how many result sets have to be shown in all
* @param
*/
public int doStartTag() throws JspException
{

HttpServletRequest request;
int numPages,startPage,endPage;
//int tempStart;
pageIndex = 1;
startPage = 1;

int total = this.total.intValue();

String uri;

//If length is not set, it is assumed
//to have been specified in the tgt:iterate tag.

length = (length == -1)?((Integer)pageContext.getAttribute("length")).intValue():length;

if(total < 0)
{
error = true;
errorMessage = "Attribute total should have a positive integer value!";
return SKIP_BODY;
}

if(total == 0)
{
return SKIP_BODY;
}

if(length <=0)
{
error = true;
errorMessage = "Attribute length should have a positive non-zero integer value!";
return SKIP_BODY;
}

numPages = (total%length == 0)?(total/length):(total/length+1);

StringBuffer contents = new StringBuffer();

if(numPages > 1)
{
request = (HttpServletRequest)pageContext.getRequest();
context = request.getContextPath();
if(request.getParameter("offset") != null)
{
pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
startPage = Integer.parseInt(request.getParameter("startPage"));
}



//Find the request URL, we have to use it again
uri = request.getRequestURI();

contents.append("

");
contents.append("Page " + Integer.toString(pageIndex) + " of " + Integer.toString(numPages) + "");

cat.debug("Showing Page " + pageIndex + " of " + numPages);
contents.append("    ");

if(pageIndex == 1)
{
contents.append("");
contents.append(" ");
contents.append("");
}
else
{
contents.append(" ");
contents.append("  ");

int prevPage = pageIndex - 1;
int prevOffSet = (prevPage -1)*length;
if(prevPage < startPage)
{
startPage--;
}

contents.append(" ");
}

contents.append("  ");


if(pageIndex == numPages)
{
contents.append("");
contents.append(" ");
contents.append("

");
}
else
{
int nextPage = pageIndex + 1;
int nextOffSet = (nextPage - 1) * length;

contents.append("");

contents.append(" ");
int lastOffSet = (numPages-1) * length;
contents.append("

");
}
contents.append("
");
} //End of numPages gt 1 check

//cat.debug(contents);

JspWriter out = pageContext.getOut();


try{
out.println(contents.toString());
}
catch(IOException ie)
{
error = true;
errorMessage = ie.getLocalizedMessage();
}

return SKIP_BODY;
}

}

Re: Show the result-set in groups

Posted By:   Erik_Runia  
Posted On:   Wednesday, May 12, 2004 04:45 AM

googling on "struts next previous" or something similar will probably turn up some turnkey or otherwise automated ways to do this.



If you are looking for a do it yourself approach, simply create an action that only returns a subset of the master arraylist based on a range passed in. For example pass the starting index and the number of items you want shown, get your arraylist as you currently do, but populate a new array based on the range given, and return that array to your form.

About | Sitemap | Contact