Displaytag On-Demand paging packaging and Instances

Source: Internet
Author: User

Displaytag1.1 and later support paging query on demand. the following descriptions are available on its official website: Displaytag 1.1 offers two alternative ways for working with partial lists: the first one uses the valuelist pattern, and requires that the object that you give to displaytag implements the org. displaytag. pagination. paginatedList interface. you can pass this object to displaytag as an usual list, and it will extract paging and sorting information from it. this Way is more recommended if you have to build your backend layer and you can easily follow this pattern. a second way, recommended if you only have to use partial list for few tables that show a performance problem using full lists, is passing all the needed parameters as separate tag attributes (recors to be shown, page number, total number of records ...) reference: http://www.displaytag.org/1.2/tut_externa LSortAndPage.html generally uses the first method when there is a large amount of data. The example in this article comes from the actual project and mainly solves two problems: 1) obtain data on demand and display the page by page using displaytag. 2) set the page parameters required by displaytag and package the complexity to facilitate rapid development. The main steps are as follows: 1. Create a simple class to implement the org. displaytag. pagination. PaginatedList interface. This class is public in the project and there is no need to create additional wheels for all paging requirement interfaces. [Java] package com. whyonly. core. displaytag; import java. util. list; import org. displaytag. pagination. paginatedList; import org. displaytag. properties. sortOrderEnum; public class SimplePaginatedList <T> implements PaginatedList {private List <T> list; private int pageNumber = 1; private int objectsPerPage = 20; private int fullListSize = 0; private String sortCriterion; private SortOrderEnum sortDirec Tion; private String searchId; public List <T> getList () {return list;} public void setList (List <T> list) {this. list = list;} public int getPageNumber () {return pageNumber;} public void setPageNumber (int pageNumber) {this. pageNumber = pageNumber;} public int getObjectsPerPage () {return objectsPerPage;} public void setObjectsPerPage (int objectsPerPage) {this. objectsPerPage = objectsPerPag E;} public int getFullListSize () {return fullListSize;} public void setFullListSize (int fullListSize) {this. fullListSize = fullListSize;} public String getSortCriterion () {return sortCriterion;} public void setSortCriterion (String sortCriterion) {this. sortCriterion = sortCriterion;} public SortOrderEnum getSortDirection () {return sortDirection;} public void setSortDirection (SortOrderEnu M sortDirection) {this. sortDirection = sortDirection;} public String getSearchId () {return searchId;} public void setSearchId (String searchId) {this. searchId = searchId ;}} 2. Create a paging package to encapsulate the common characteristics according to the displaytag paging requirements. At the same time, let the subclass process the different parts through the interface, this package is also used in projects. [Java] package com. whyonly. core. displaytag; import java. util. list; import javax. servlet. http. httpServletRequest; public abstract class PaginatedWrapper <T> {private int pageSize = 20; public void paginated (HttpServletRequest request) {int page = 1; if (request. getParameter ("pageSize ")! = Null &&! "". Equals (request. getParameter ("pageSize") {pageSize = Integer. parseInt (request. getParameter ("pageSize");} else {pageSize = 20;} if (request. getParameter ("page ")! = Null &&! "". Equals (request. getParameter ("page") {page = Integer. parseInt (request. getParameter ("page");} else {page = 1;} int fromIndex = (page-1) * pageSize; int toIndex = fromIndex + pageSize; int fullListSize = getFullListSize (); List <T> pageDatas = getPageDatas (fromIndex, toIndex); SimplePaginatedList <T> paginatedList = new vertex <T> (); paginatedList. setPageNumber (page); if (pageDat As! = Null & pageDatas. size ()> 0 & fullListSize> 0) {paginatedList. setFullListSize (fullListSize); paginatedList. setObjectsPerPage (pageSize); paginatedList. setList (pageDatas);} else {paginatedList. setFullListSize (0); paginatedList. setList (null);} request. setAttribute ("pagedatas", paginatedList);} public PaginatedWrapper <T> setPageSize (int pageSize) {this. pageSize = pageSize; return this;} pro Tected abstract List <T> getPageDatas (int fromIndex, int toIndex); protected abstract int getFullListSize ();} PaginatedWrapper is an abstract class with the following features: 1) the wildcard type <T> is supported. bean objects can be imported according to the requirements of the module. 2) The paginated () method aggregates and encapsulates the displaytag parameter requirements, and performs some common initialization. At the same time, you can set the pagesize. The default value is 20. Note that setPageSize is set back to this. In this example, we use the reverse method to set consecutive attributes. 3) The abstract METHODS protected abstract List <T> getPageDatas (int fromIndex, int toIndex) and protected abstract int getFullListSize () have different characteristics for each module, therefore, the interface method is left to a specific module for implementation. The implementation is generally placed on the Dao layer and then called through the front-end Controller (Spring), Action (Struts), or Jsp. 3. Use the two packaging classes created in step 1 and Step 2 to apply them to a specific project. The following uses SSH2 as an example and can be applied to other framework structures. Because the SSH framework is common, only key code is provided below. The specific implementation should be simple. 3.1, Action class [java] @ Actions ({@ Action (value = "/queryIncidental", results = {@ Result (location = "center/pos/incidental_query_result.jsp ", name = "success"),}) public String queryIncidental () {new PaginatedWrapper <Posbatch> () {@ Override protected List <Posbatch> getPageDatas (int fromIndex, int toIndex) {return service. initQueryIncidentalByPage (mv, getCompid (), fromIndex, toIndex) ;}@ Overr Ide protected int getFullListSize () {return service. findFullSizeByCustAndDate (mv, getCompid ());}}. paginated (request); return SUCCESS;} the key point of the preceding Action is to implement the getPageDatas () and getFullListSize () methods. The service is the business logic as an object. If Spring is used, you can use @ Autowired for automatic injection. 3.2) Service class [java] public List <Posbatch> initQueryIncidentalByPage (IncidentalMV, int compid, int fromIndex, int toIndex) {String custcode = mv. getCustcode (); KTimestamp start = Kalendar. stringToKTimestamp ("yyyy-MM-dd", mv. getStartdate (); KTimestamp end = Kalendar. stringToKTimestamp ("yyyy-MM-dd", mv. getEnddate (); return posbatchDao. findByCustAndDateForPage (fromIndex, toIndex, compid, custcode, Start, Kalendar. getKTimestampAfterDays (end, 1);} public int findFullSizeByCustAndDate (IncidentalMV, int compid) {String custcode = mv. getCustcode (); KTimestamp start = Kalendar. stringToKTimestamp ("yyyy-MM-dd", mv. getStartdate (); KTimestamp end = Kalendar. stringToKTimestamp ("yyyy-MM-dd", mv. getEnddate (); return posbatchDao. findFullSizeByCustAndDate (compid, custcode, start, end);} 3.3) Dao class [Java] public List <Posbatch> findByCustAndDateForPage (int fromIndex, int toIndex, int compid, String custcode, KTimestamp start, KTimestamp end) {StringBuffer buff = new StringBuffer (); buff. append ("select p from Posbatch p left join fetch p. customer c left join fetch p. wicustomer w left join fetch p. createPerson cp "+" where p. compid =? And p. ctime >=? And p. ctime <? "); Buff. append ("order by p. batchnum desc "); return super. find (buff. toString (), fromIndex, toIndex, compid, start, end);} public int findFullSizeByCustAndDate (int compid, String custcode, KTimestamp start, KTimestamp end) {StringBuffer buff = new StringBuffer (); buff. append ("select count (*) from Posbatch p" + "where p. compid =? And p. ctime >=? And p. ctime <? "); Buff. append (custcode! = Null &&! Custcode. equals ("")? "And p. custcode =? ":" "); Buff. append ("order by p. batchnum desc "); return super. findFullSize (buff. toString (), compid, start, end);} the Dao uses the left join fetch method of Hibernate. Therefore, the associated objects of Posbatch are directly injected to facilitate display of displaytags. In addition, this Dao inherits its own BaseHibernateDAO, findFullSize (), and the Code for the find () method is as follows: [java] public List <T> find (String hql, int fromIndex, int toIndex, Object... params) {Query query = getSession (). createQuery (hql); for (int I = 0, len = params. length; I <len; I ++) {query. setParameter (I, params [I]);} query. setFirstResult (fromIndex); query. setMaxResults (toIndex-fromIndex); return query. list ();} public int findFullSi Ze (String hql, Object... params) {Query query = getSession (). createQuery (hql); for (int I = 0, len = params. length; I <len; I ++) {query. setParameter (I, params [I]);} return (Long) query. list (). get (0 )). intValue ();} 3.4) page display (JSP) [html] <display: table id = "pagedatas" name = "pagedatas" export = "false" sort = "external" class = "dispaytag" requestURI = "queryIncidental" decorator = "com. whyonly. center. pos. m V. posbatchWrapper "> <display: column property =" batchnum "paramId =" batchnum "paramProperty =" batchnum "href =" incidental "titleKey =" centerowl. posinvoicing. searchtran2 "/> <display: column property =" custname "titleKey =" centerowl. posinvoicing. searchtran3 "/> <display: column property =" ctime "titleKey =" centerowl. posinvoicing. searchtran4 "/> <display: column property =" createPerson. name "titleKey =" centerowl. Posinvoicing. searchtran5 "/> </display: table> a wrapper is used to display the date. If you do not need to perform special processing on the data, ignore it directly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.