Java Paging component Simple design (thief simple)

Source: Internet
Author: User

The code is simple, the key is thought.

Each business module basically involves paging applications, whether it's a normal paging or waterfall stream form of browsing. Paging can reduce the pressure on the server, you can save the page layout. In our pagination simple component design, we mainly need to return four data to the client: List object, current page number, maximum number of records per page, total number of records.

List object: As the name implies, is to show the user to see the current page record;

Current page: The main need to be JS to highlight;

Maximum number of records per page: typically server settings

Total records: JS used to calculate the total number of pages used


Design ideas:

1. Because paging is a publicly available feature, it can be developed into a component that avoids the developer's ability to customize the field names of the four data above, causing maintenance headaches. This can actually be encapsulated into a method that defines four field names in the method. After considering the list object's field name should have its own actual meaning, so customize it, in the method only define the last three fields.

/** * Assembly Page List * @param t * @param totalcount * @param page * @param length * @return */@SuppressWarnings ("unchecked") Protec Ted void Setpagelist (Object t, integer totalcount, integer page,integer length) {if (T instanceof Map) {map<string, objec t> map = (map<string, object>) t;map.put ("TotalCount", TotalCount); Map.put ("page", page); Map.put ("Length", length);} else if (t instanceof model) {Model model = (model) t;model.addattribute ("TotalCount", TotalCount); Model.addattribute (" Page ", page); Model.addattribute (" Length ", length);}}
The TotalCount, page, and length three fields are defined to indicate the total number of records, the current page number, and the maximum number of records per page.

2. Above is the name of the field that defines the return to the client. Another thing to note is how to get the list of records and the total number of records that match the criteria.

This can be achieved separately, using two methods to get a list of records and the total number of records (if there is a cache list, it is convenient to get two data, otherwise, only the interactive database). This approach is required every time in the business layer to take two of data from the service layer, if once the service layer is deployed independently, then the result is that each paging to the network to transmit two data, this is not a good way. It is decided to encapsulate a bean that contains both fields so that the paging bean will be returned at the service layer so that only one network transfer is needed to get the data for paging.

The code is as follows:

public class Pagelistresult {private list<?> list;private long totalcount;public list<?> getList () {return Li St;} public void setlist (list<?> list) {this.list = list;} Public long Gettotalcount () {return totalcount;} public void Settotalcount (long totalcount) {this.totalcount = TotalCount;}}

This allows for a simple paging component.

Rookie level, Daniel do not spray ~ humbly!






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.