[导入][AppFuse] AppFuse使用手记--Display Tag 分页(九) [原]
AppFuse使用 Display Tag做为内容显示。Display Tag的分页默认是按全量数据自动进行分页。如果数据量大的话,就很耗费性能。
可以参考 External Paging and Sorting。
如果少量数据可以通过Tag的参数进行分页,大数据量的化,可以采用Valuelist pattern,对象继承org.displaytag.pagination.PaginatedList实现分页和排序。
那么什么是Valuelist pattern?
Valuelist patten(值列表处理器)是J2EE核心模式中的一种。简单点说明就是,Client(客户端)通过访问ValueListHandler(值列表处理器)获得ValueListIterator(值列表迭代器),ValueListHandle则通调用DataAccessObject(数据访问对象)。
在我们的实现里,Client应该是JSP,ValueListHandle则是Action,DataAccessObject最后的实现是HibernateDAO。
Client(companyList.jsp):
ValueListHandle(CompanySearchAction):
DataAccessObject(CompanyDaoHibernate):
文章来源: http://heyday.blogcn.com/diary,15576279.shtml
可以参考 External Paging and Sorting。
如果少量数据可以通过Tag的参数进行分页,大数据量的化,可以采用Valuelist pattern,对象继承org.displaytag.pagination.PaginatedList实现分页和排序。
那么什么是Valuelist pattern?
Valuelist patten(值列表处理器)是J2EE核心模式中的一种。简单点说明就是,Client(客户端)通过访问ValueListHandler(值列表处理器)获得ValueListIterator(值列表迭代器),ValueListHandle则通调用DataAccessObject(数据访问对象)。
在我们的实现里,Client应该是JSP,ValueListHandle则是Action,DataAccessObject最后的实现是HibernateDAO。
Client(companyList.jsp):
1
<
display:table
name
="ph"
class
="table"
requestURI
=""
id
="ph"
export
="true"
>
2 < display:column property ="companyId" sortable ="true" href ="editCompany.html" media ="html"
3 paramId ="companyId" paramProperty ="companyId" titleKey ="company.companyId" />
4 < display:column property ="companyId" media ="csv excel xml pdf" titleKey ="company.companyId" />
5 < display:column sortProperty ="status" sortable ="true" titleKey ="company.status" >
6 < input type ="checkbox" disabled ="disabled" <c:if test ="${companyList.status}" > checked="checked" </ c:if > />
7 </ display:column >
8
9 < display:column property ="companyType.typeName" sortable ="true" titleKey ="companyType.typeName" />
10 < display:column property ="companyName" sortable ="true" titleKey ="company.companyName" />
11
12 < display:setProperty name ="paging.banner.item_name" >< fmt:message key ="companyList.company" /></ display:setProperty >
13 < display:setProperty name ="paging.banner.items_name" >< fmt:message
14 key ="companyList.companies" /></ display:setProperty >
15
16 < display:setProperty name ="export.excel.filename" >< fmt:message key ="companyList.title" /> .xls </ display:setProperty >
17 < display:setProperty name ="export.csv.filename" >< fmt:message key ="companyList.title" /> .csv </ display:setProperty >
18 < display:setProperty name ="export.pdf.filename" >< fmt:message key ="companyList.title" /> .pdf </ display:setProperty >
19 </ display:table >
2 < display:column property ="companyId" sortable ="true" href ="editCompany.html" media ="html"
3 paramId ="companyId" paramProperty ="companyId" titleKey ="company.companyId" />
4 < display:column property ="companyId" media ="csv excel xml pdf" titleKey ="company.companyId" />
5 < display:column sortProperty ="status" sortable ="true" titleKey ="company.status" >
6 < input type ="checkbox" disabled ="disabled" <c:if test ="${companyList.status}" > checked="checked" </ c:if > />
7 </ display:column >
8
9 < display:column property ="companyType.typeName" sortable ="true" titleKey ="companyType.typeName" />
10 < display:column property ="companyName" sortable ="true" titleKey ="company.companyName" />
11
12 < display:setProperty name ="paging.banner.item_name" >< fmt:message key ="companyList.company" /></ display:setProperty >
13 < display:setProperty name ="paging.banner.items_name" >< fmt:message
14 key ="companyList.companies" /></ display:setProperty >
15
16 < display:setProperty name ="export.excel.filename" >< fmt:message key ="companyList.title" /> .xls </ display:setProperty >
17 < display:setProperty name ="export.csv.filename" >< fmt:message key ="companyList.title" /> .csv </ display:setProperty >
18 < display:setProperty name ="export.pdf.filename" >< fmt:message key ="companyList.title" /> .pdf </ display:setProperty >
19 </ display:table >
ValueListHandle(CompanySearchAction):
1
public
String search()
{
2 HttpServletRequest request = getRequest();
3 String strPage = request.getParameter("page");
4 int page = strPage == null ? 1 : new Integer(strPage).intValue();
5
6 PaginatedListHelper ph = new PaginatedListHelper();
7 int size = ph.getObjectsPerPage();
8 int index = (page-1)* size;
9 companies = companyDao.find(company, index,size);
10
11 int count = companyDao.getSize(company);
12
13 ph.setFullListSize(count);
14 ph.setList(companies);
15 ph.setPageNumber(page);
16
17 request.setAttribute("ph", ph);
18
19 return SUCCESS;
20 }
2 HttpServletRequest request = getRequest();
3 String strPage = request.getParameter("page");
4 int page = strPage == null ? 1 : new Integer(strPage).intValue();
5
6 PaginatedListHelper ph = new PaginatedListHelper();
7 int size = ph.getObjectsPerPage();
8 int index = (page-1)* size;
9 companies = companyDao.find(company, index,size);
10
11 int count = companyDao.getSize(company);
12
13 ph.setFullListSize(count);
14 ph.setList(companies);
15 ph.setPageNumber(page);
16
17 request.setAttribute("ph", ph);
18
19 return SUCCESS;
20 }
DataAccessObject(CompanyDaoHibernate):
1
public
int
getSize(
final
Company company)
{
2 String queryString = "select count(*) from Company where 1 = 1";
3 ArrayList<Serializable> values = new ArrayList<Serializable>();
4 if (company.getCompanyName() != null && !company.getCompanyName().equals("")) {
5 queryString += " and companyName like ?";
6 values.add("%" + company.getCompanyName() + "%");
7 }
8
9 if (company.getCompanyType().getTypeId() != null) {
10 queryString += " and companyType = ?";
11 values.add(company.getCompanyType());
12 }
13
14 Object[] objects = values.toArray();
15
16 List list = this.getHibernateTemplate().find(queryString, objects);
17 Long count = (Long) list.get(0);
18 return count.intValue();
19 }
20
21 public List find( final Company company, final int index, final int size) {
22
23 String sql = "from Company where 1 = 1";
24 if (company.getCompanyName() != null && !company.getCompanyName().equals("")) {
25 sql += " and companyName like :companyName";
26 }
27 if (company.getCompanyType().getTypeId() != null) {
28 sql += " and companyType = :companyType";
29 }
30 final String queryString = sql;
31
32 return (List) getHibernateTemplate().execute(new HibernateCallback() {
33 public Object doInHibernate(Session session)
34 throws HibernateException {
35
36 Query query = session.createQuery(queryString);
37
38 if (company.getCompanyName() != null && !company.getCompanyName().equals("")) {
39 String companyName = "%" + company.getCompanyName() + "%";
40 query.setParameter("companyName", companyName);
41 }
42 if (company.getCompanyType().getTypeId() != null) {
43 query.setParameter("companyType", company.getCompanyType());
44 }
45
46 query.setFirstResult(index).setMaxResults(size);
47 return query.list();
48 }
49 });
50 }
2 String queryString = "select count(*) from Company where 1 = 1";
3 ArrayList<Serializable> values = new ArrayList<Serializable>();
4 if (company.getCompanyName() != null && !company.getCompanyName().equals("")) {
5 queryString += " and companyName like ?";
6 values.add("%" + company.getCompanyName() + "%");
7 }
8
9 if (company.getCompanyType().getTypeId() != null) {
10 queryString += " and companyType = ?";
11 values.add(company.getCompanyType());
12 }
13
14 Object[] objects = values.toArray();
15
16 List list = this.getHibernateTemplate().find(queryString, objects);
17 Long count = (Long) list.get(0);
18 return count.intValue();
19 }
20
21 public List find( final Company company, final int index, final int size) {
22
23 String sql = "from Company where 1 = 1";
24 if (company.getCompanyName() != null && !company.getCompanyName().equals("")) {
25 sql += " and companyName like :companyName";
26 }
27 if (company.getCompanyType().getTypeId() != null) {
28 sql += " and companyType = :companyType";
29 }
30 final String queryString = sql;
31
32 return (List) getHibernateTemplate().execute(new HibernateCallback() {
33 public Object doInHibernate(Session session)
34 throws HibernateException {
35
36 Query query = session.createQuery(queryString);
37
38 if (company.getCompanyName() != null && !company.getCompanyName().equals("")) {
39 String companyName = "%" + company.getCompanyName() + "%";
40 query.setParameter("companyName", companyName);
41 }
42 if (company.getCompanyType().getTypeId() != null) {
43 query.setParameter("companyType", company.getCompanyType());
44 }
45
46 query.setFirstResult(index).setMaxResults(size);
47 return query.list();
48 }
49 });
50 }
文章来源: http://heyday.blogcn.com/diary,15576279.shtml