My favorites | Sign in
Project Logo
                
Search
for
Updated Feb 17, 2009 by kuisong.tong
Pagination  
transparent pagination base on presentation parameters.

Introduction

We always need pagination when listing objects in presentation, how to let system do this for you? Base on Searcher on search object, we can do this transparently.

Setup

First, we need a spring bean which implemented PaginationSource tell system how to get the start and limit value for pagination, and where to hold the total count of this search. Normally, we can get pagination info from http request parameters, now we just using a mocked one.

@Component
public class MockPaginationSource implements PaginationSource {
	private long totalCount;
	
	public Pagination getPagination() {
		return new Pagination(1, 1);
	}

	public void setTotalCount(long totalCount) {
		this.totalCount = totalCount;
	}

	public long getTotalCount() {
		return totalCount;
	}

	public List<Order> getOrders() {
		List<Order> orders = new ArrayList<Order>();
		orders.add(Order.ascending("createdTime"));
		return orders;
	}
}

Details

First, we add count method for search in UserRepository: We add delegator for search in UserService:

 	@Counter("User.search") 
 	long count(UserSO userSO);        

The "User.search" in Count annotation tell system using which query statement to get the total records count. Second, we add delegator for search and count in UserService:

 	@Pager        
 	@Delegator(bean = "userRepository", value = "findBySO") 
	List<UserDto> findUser(UserSO userSO); 
 	
 	@Delegator(bean = "userRepository", value = "countBySO")        
 	long countUser(UserSO userSO);        

The magic annotation is @Pager, which tell system to execute count method when invoking search method, and pass the pagination info to dao automatically.

That's all.


Sign in to add a comment
Hosted by Google Code