[jboss-user] [JBoss Seam] - Re: How to impliment a Paged + Sorted DataTable (session) wi

antispart do-not-reply at jboss.com
Fri Nov 17 11:52:08 EST 2006


I found something that works for me. I'm including it here in case it might help someone else (or perhaps someone could suggest a better solution):


  | @Stateful
  | @Name("testAction")
  | @Scope(ScopeType.CONVERSATION)
  | public class TestActionBean implements TestActionLocal {
  |     @Logger private Log log;
  |        
  |     @In(create=true)
  |     private EntityManager entityManagerSeam;
  | 
  |     private String searchString;
  |     private int pageSize = 10;
  |     private int page = 0;
  |     private int pageCount = 0;    
  |     private String column = "testcolumn";
  |     private boolean ascending = true;
  |     private boolean refresh = false;
  | 
  |     public String find()   {
  | 		setRefresh(true);
  | 		return null;
  |     }
  |     
  |     public String sort(String newcolumn) {
  | 		if (column.equals(newcolumn))
  | 	 	   ascending = !ascending;
  | 		column = newcolumn;
  | 		return find();
  |     }
  |     
  |     public boolean isRefresh() {
  | 		return refresh;
  |     }
  |     
  |     public void setRefresh(boolean refresh) {
  | 		this.refresh = refresh;
  |     }
  |     
  |     
  |     public String firstPage() {
  | 		page = 0;
  | 		return find();
  |     }
  |     
  |     public String previousPage() {
  | 		page--;
  | 		return find();
  |     }
  |     
  |     public String nextPage() {
  | 		page++;
  | 		return find();
  |     }
  |     
  |     public String lastPage() {
  | 		page = pageCount - 1;
  | 		return find();
  |     }
  |     
  |     public int getPage() {
  | 		return page;
  |     }
  |     
  |     public void setPage(int page) {
  | 		this.page = page;
  |     }
  |     
  |     public int getPageCount() {
  | 		return pageCount;
  |     }
  |     
  |     public void setPageCount(int pageCount) {
  | 		this.pageCount = pageCount;
  |     }
  |     
  |     public int getPageSize() {
  | 		return pageSize;
  |     }
  |     
  |     public void setPageSize(int pageSize) {
  | 		this.pageSize = pageSize;
  |     }
  |     
  |     public boolean isAscending() {
  | 		return ascending;
  |     }
  |     
  |     public String getColumn() {
  | 		return column;
  |     }
  |     
  |     public String getSearchString() {
  | 		return searchString;
  |     }
  |     
  |     public void setSearchString(String searchString) {
  | 		this.searchString = searchString;
  |     }
  |     
  |     public List get() {
  | 		...
  | 	
  | 		return entityManagerSeam.createQuery("...")
  | 			.setParameter("search", searchPattern)
  | 			.setMaxResults(pageSize)
  | 			.setFirstResult(page * pageSize)
  | 			.getResultList();
  |     }
  | 


  | @Name("testList")
  | @Scope(ScopeType.PAGE)
  | public class TestList implements java.io.Serializable {
  |     @Logger private Log log;
  |     
  |     @In(create=true)
  |     private FacesMessages facesMessages;
  |     
  |     @In(create=true)
  |     private TestActionLocal testAction;
  | 
  |     private List testList;
  |     
  |     private void refresh() {
  | 		try {
  | 		    testList = (List) testAction.get();
  | 		} catch (Exception e) {
  | 		    log.error("Exception", e);
  | 		    facesMessages.addFromResourceBundle("errors_List");
  | 		}
  |     }
  |     
  |     @Unwrap
  |     public List create() {
  | 		if (testList==null || testAction.isRefresh()) {
  | 		    testAction.setRefresh(false);
  | 	 	   refresh();
  | 		}
  | 		return testList;
  |     }
  | }
  | 

With this setup ajax method invocations do not cause a new DB call (do not refresh the list) unless they're specifically meant to. The list is refreshed every time the page is accessed (but only once - by the testList==null test before hitting the db).

This works well for me and was the cleanest pattern I could find. Improvements very welcome though.

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3986901#3986901

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3986901



More information about the jboss-user mailing list