应用displaytag完成大数据量分页显示的例子

应用displaytag在struts中完成大数据量分页显示,Oracle数据库

JSP文件:

<% ... @ taglib uri = " /WEB-INF/displaytag.tld "  prefix = " disp "   %>

 < disp:table  name ="resultList"  export ="true"  pagesize ="100"  requestURI ="logQueryAction.do"  sort ="external"  id ="element"  partialList ="true"  size ="resultSize" >
            < disp:column  property ="operdate"  title ="操作时间"   ></ disp:column >
            < disp:column  property ="pername"  title ="操作人员"   ></ disp:column >
            < disp:column  property ="opertype"  title ="操作类型"   ></ disp:column >
 </ disp:table > 
 
name="resultList" 将记录集存在session或者request中的键值
export="true" 是否显示导出选项
pagesize="100" 每页显示100条数据
requestURI="logQueryAction.do"  struts中action的名称,如果记录少,可以直接分页
sort="external"  外部排序
id="element"   表格id值,用于程序得相关的参数
partialList="true"  分段从数据库中读数据
size="resultSize"  记录的总条数,用于计算总页数

struts action:

String pageIndexName  =   new  org.displaytag.util.ParamEncoder( " element " ).encodeParameterName(org.displaytag.tags.TableTagParameters.PARAMETER_PAGE);   // 页数的参数名
          int  pageSize  =   100 ;   //每页显示的条数
          int  pageIndex  =  GenericValidator.isBlankOrNull(request.getParameter(pageIndexName)) ? 0 :(Integer.parseInt(request.getParameter(pageIndexName))  -   1 );  //当前页数
 String sqlCount  =   " select count(*)  from user_log a  " ;  //用于统计总记录数的sql语句
         String sql  =   "  select * from (select rownum as rid, t1.* from (select b.pername as pername,to_char(a.operdate,'yyyy-mm-dd hh24:mi:ss') as operdate, "   +
                  "  decode(a.opertype,'D','删除','M','修改','其他 ') as opertype, a.hphm as hphm from user_log a,  "   +
                  "  (select asuser.userid as userid,nvl(asempmsg.pername,asuser.loginname) as pername from asuser,ASEMPMSG where asuser.perid=ASEMPMSG.perid(+)) b "   +
                  "  where a.userid=b.userid  " ;   // 查询语句
            //构造查询条件
         StringBuffer sb  =   new  StringBuffer();
          if (logQueryForm.getCzrqStart() != null   &&   ! "" .equals(logQueryForm.getCzrqStart())) ... {
             sb.append( "  and a.operdate > to_date(' " + logQueryForm.getCzrqStart() + " ','yyyy-mm-dd') " );
         }
          if (logQueryForm.getCzrqEnd() != null   &&   ! "" .equals(logQueryForm.getCzrqEnd())) ... {
             sb.append( "  and a.operdate <= to_date(' " + logQueryForm.getCzrqEnd() + " ','yyyy-mm-dd') " );
         }
          if (logQueryForm.getCzlx() != null   &&   ! "" .equals(logQueryForm.getCzlx())) ... {
             sb.append( "  and a.opertype = ' " + logQueryForm.getCzlx() + " ' " );
         }
          if (logQueryForm.getCzry() != null   &&   ! "" .equals(logQueryForm.getCzry())) ... {
             sb.append( "  and a.userid = ' " + logQueryForm.getCzry() + " ' " );
         }
      
         sqlCount  +=  sb.toString();
         sql  +=  sb.toString() + "  order by a.operdate desc) t1 where rownum<= "
                          +  (pageIndex  +   1 )  *  pageSize  +   "  ) t2 where t2.rid> " +  pageIndex  *  pageSize;  //分页读取语句
        
          // System.out.println(sb.toString());
         DBBean db  =   new  DBBean();
         ResultSet rs  =   null ;
         PreparedStatement prep  =   null ;
          try ... {
             List resultList  =  db.getResultList(sql);   //将ResultSet保存在List里返回
             request.setAttribute( " resultList " ,resultList);  //把结果存入request
             prep  =  db.getConnection().prepareStatement(sqlCount);   
             rs  =  prep.executeQuery();
              if (rs.next()) ... {
                 request.setAttribute( " resultSize " , new  Integer(rs.getInt( 1 )));     //将总记录数保存成Intger实例保存在request中
             }
         }
          catch (Exception ex) ... {
             ex.printStackTrace();
         }
          finally ... {
              if (db != null ) ... {
                 db.closeConnection();    //关闭连接
             } 
         }
其中的页面导航是英文的,只要修改org.displaytag.properties.TableTag.properties配置文件就可以把英文改成中文。同时还可以指定导出文件的类型以及文件名。

export.excel = true
 export.excel.label = <span class = " export excel " >Excel </span>
 export.excel.include_header = true
 export.excel.filename = export.xls
如果不指定文件名,在点击导出excel的时候,就会在ie窗口中打开excel文件。很烦人!
指定文件名后就可以选择保存和打开了。

< type="text/javascript"><!-- google_ad_client = "pub-9648238652058580"; google_ad_width = 468; google_ad_height = 60; google_ad_format = "468x60_as_rimg"; google_cpa_choice = "CAAQiYaYhAIaCJ2wcuQYTrQ_KOm293M"; google_ad_channel = ""; //-->< type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">

posted @ 2010-08-17 12:12  chinaifne  阅读(...)  评论(...编辑  收藏