急。。。能用JasperReport生成html页面,但是不知道如何进行分页,请知道的话帮帮忙!
这是我的Action代码:
public ActionForward html(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws
IOException {
ServletContext context = this.getServlet().getServletConfig().getServletContext();
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Connection connection;
Statement statement;
ResultSet resultSet;
try{
int pageIndex = 0;
File reportFile = new File(context.getRealPath("/reports/staffer.jasper"));
JasperReport jasperReport= (JasperReport)
JRLoader.loadObject(reportFile.getPath());
StringBuffer sb=new StringBuffer();
sb.append(" select * from staffer");
Class.forName("net.sourceforge.jtds.jdbc.Driver");
connection=DriverManager.getConnection("jdbc:jtds:sqlserver://localhost/yq", "sa", "123");
statement=connection.createStatement();
resultSet=statement.executeQuery(sb.toString());
JRResultSetDataSource resultSetDataSourde=new JRResultSetDataSource(resultSet);
JasperPrint jasperPrint=JasperFillManager.fillReport(jasperReport,new HashMap(),resultSetDataSourde);
JRHtmlExporter exporter = new JRHtmlExporter();
StringBuffer sbuffer = new StringBuffer();
request.setAttribute("sb", sbuffer);
exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER, sbuffer);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
exporter.setParameter(JRExporterParameter.PAGE_INDEX, new Integer(pageIndex));
exporter.exportReport();
}catch(Exception e){
e.printStackTrace();
}
return mapping.findForward("html");
}
我显示转到另外view.jsp页面上输出:
<%=request.getAttribute("sb").toString()%>,当然页面上只是显示第一页。
那我现在怎么在view.jsp进行分页,也就是说可以点下一页进行预览,请高手指点。。。。
------解决方案--------------------分页
给你个分页bean研究一下就ok了
Java code
import java.io.*;
public class PageBean implements Serializable
{
private int currentPage;
private int totalItem;
private int everyPage;
//设置当前页
public void setCurrentPage(int currentPage)
{
int totalPage = this.getTotalPage();
if(currentPage <= 0)
{
this.currentPage = 1;
} else if(currentPage >= totalPage)
{
this.currentPage = totalPage ;
}
this.currentPage = currentPage;
}
//取得当前页
public int getCurrentPage()
{
if(totalItem <= 0)
{
return 1;
}
return this.currentPage;
}
//取得当前页码
public int getCurrentPageNum() {
if(totalItem <= 0) {
return 1;
}
return this.currentPage ;
}
public void setEveryPage(int everyPage)
{
this.everyPage = everyPage;
}
public int getEveryPage()
{
return this.everyPage;
}
public int getTotalPage()
{
if(totalItem % everyPage