1 7 package org.jfox.petstore.action; 8 9 import java.util.List ; 10 import javax.ejb.EJB ; 11 12 import org.jfox.petstore.bo.AccountBO; 13 import org.jfox.petstore.bo.CategoryBO; 14 import org.jfox.petstore.bo.ProductBO; 15 import org.jfox.petstore.entity.Product; 16 import org.jfox.petstore.entity.Category; 17 import org.jfox.mvc.util.PagedList; 18 import org.jfox.framework.annotation.Service; 19 import org.jfox.mvc.ActionSupport; 20 import org.jfox.mvc.Invocation; 21 import org.jfox.mvc.InvocationContext; 22 import org.jfox.mvc.PageContext; 23 import org.jfox.mvc.SessionContext; 24 import org.jfox.mvc.annotation.ActionMethod; 25 26 29 @Service(id = "category") 30 public class CategoryAction extends ActionSupport { 31 32 @EJB 33 AccountBO accountBO; 34 35 @EJB (name = "CategoryBOImpl") 36 CategoryBO categoryBO; 37 38 @EJB 39 ProductBO productBO; 40 41 public void postInject() { 42 super.postInject(); 43 } 44 45 51 @ActionMethod(successView = "Category.vhtml", invocationClass = CategoryInvocation.class) 52 public void doGetView(InvocationContext invocationContext) throws Exception { 53 CategoryInvocation invocation = (CategoryInvocation)invocationContext.getInvocation(); 54 Category category = categoryBO.getCategory(invocation.getCategoryId()); 55 56 PageContext pageContext = invocationContext.getPageContext(); 57 SessionContext sessionContext = invocationContext.getSessionContext(); 58 59 PagedList<Product> productPagedList; 60 61 List <Product> products = productBO.getProductsByCategory(invocation.getCategoryId()); 64 productPagedList = new PagedList<Product>(products, 4); 65 sessionContext.setAttribute("ProductPageList", productPagedList); 66 for(int i=0; i< invocation.getPage(); i++){ 71 productPagedList.nextPage(); 72 } 73 74 int nextPage = invocation.getPage(); 75 if(!productPagedList.isLastPage()){ 76 nextPage++; 77 } 78 79 int previousPage = invocation.getPage(); 80 if(!productPagedList.isFirstPage()){ 81 previousPage--; 82 } 83 84 92 93 97 pageContext.setAttribute("account", sessionContext.getAttribute("account")); 98 pageContext.setAttribute("category", category); 99 pageContext.setAttribute("categoryId", invocation.getCategoryId()); 100 pageContext.setAttribute("pageList", productPagedList); 101 pageContext.setAttribute("previousPage", previousPage); 102 pageContext.setAttribute("nextPage", nextPage); 103 104 } 105 106 public static class CategoryInvocation extends Invocation { 107 108 private String categoryId = "BIRDS"; 109 110 private int page = 0; 111 112 public String getCategoryId() { 113 return categoryId; 114 } 115 116 public void setCategoryId(String categoryId) { 117 this.categoryId = categoryId; 118 } 119 120 121 public int getPage() { 122 return page; 123 } 124 125 public void setPage(int page) { 126 this.page = page; 127 } 128 } 129 130 public static void main(String [] args) { 131 132 } 133 } 134 | Popular Tags |