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.ItemBO; 13 import org.jfox.petstore.bo.ProductBO; 14 import org.jfox.petstore.entity.Item; 15 import org.jfox.petstore.entity.Product; 16 import org.jfox.framework.annotation.Service; 17 import org.jfox.mvc.ActionSupport; 18 import org.jfox.mvc.Invocation; 19 import org.jfox.mvc.InvocationContext; 20 import org.jfox.mvc.PageContext; 21 import org.jfox.mvc.util.PagedList; 22 import org.jfox.mvc.annotation.ActionMethod; 23 24 27 @Service(id = "product") 28 public class ProductAction extends ActionSupport { 29 30 @EJB 31 ItemBO itemBO; 32 33 @EJB 34 ProductBO productBO; 35 36 @ActionMethod(successView = "Product.vhtml", invocationClass = ProductInvoation.class) 37 public void doGetView(InvocationContext invocationContext) { 38 ProductInvoation invocation = (ProductInvoation)invocationContext.getInvocation(); 39 40 Product product = productBO.getProduct(invocation.getProductId()); 41 42 List <Item> items = itemBO.getItemListByProduct(invocation.getProductId()); 43 PagedList<Item> itemPageList = new PagedList<Item>(items, 4); 44 for (int i = 0; i < invocation.getPage(); i++) { 45 itemPageList.nextPage(); 46 } 47 48 int nextPage = invocation.getPage(); 49 if (!itemPageList.isLastPage()) { 50 nextPage++; 51 } 52 53 int previousPage = invocation.getPage(); 54 if (!itemPageList.isFirstPage()) { 55 previousPage--; 56 } 57 58 PageContext pageContext = invocationContext.getPageContext(); 59 pageContext.setAttribute("product", product); 60 pageContext.setAttribute("productId", invocation.getProductId()); 61 pageContext.setAttribute("itemPageList", itemPageList); 62 pageContext.setAttribute("previousPage", previousPage); 63 pageContext.setAttribute("nextPage", nextPage); 64 65 } 66 67 @ActionMethod(successView = "SearchProducts.vhtml", invocationClass = SearchProductInvocation.class) 68 public void doPostSearch(InvocationContext invocationContext) { 69 SearchProductInvocation invocation = (SearchProductInvocation)invocationContext.getInvocation(); 70 String keyword = invocation.getKeyword(); 71 String [] keywords = keyword.split(" "); 72 List <Product> products = productBO.searchProductList(keywords); 73 PagedList<Product> productPagedList = new PagedList<Product>(products,4); 74 75 for (int i = 0; i < invocation.getPage(); i++) { 76 productPagedList.nextPage(); 77 } 78 79 int nextPage = invocation.getPage(); 80 if (!productPagedList.isLastPage()) { 81 nextPage++; 82 } 83 84 int previousPage = invocation.getPage(); 85 if (!productPagedList.isFirstPage()) { 86 previousPage--; 87 } 88 89 PageContext pageContext = invocationContext.getPageContext(); 90 pageContext.setAttribute("productPageList", productPagedList); 91 pageContext.setAttribute("previousPage", previousPage); 92 pageContext.setAttribute("nextPage", nextPage); 93 } 94 95 @ActionMethod(successView = "SearchProducts.vhtml", invocationClass = SearchProductInvocation.class) 96 public void doGetSearch(InvocationContext invocationContext) { 97 doPostSearch(invocationContext); 98 } 99 100 public static class ProductInvoation extends Invocation { 101 private String productId; 102 103 private int page = 0; 104 105 public String getProductId() { 106 return productId; 107 } 108 109 public void setProductId(String productId) { 110 this.productId = productId; 111 } 112 113 public int getPage() { 114 return page; 115 } 116 117 public void setPage(int page) { 118 this.page = page; 119 } 120 } 121 122 public static class SearchProductInvocation extends Invocation { 123 124 private String keyword = ""; 125 126 private int page = 0; 127 128 public String getKeyword() { 129 return keyword; 130 } 131 132 public void setKeyword(String keyword) { 133 this.keyword = keyword; 134 } 135 136 public int getPage() { 137 return page; 138 } 139 140 public void setPage(int page) { 141 this.page = page; 142 } 143 } 144 145 public static void main(String [] args) { 146 147 } 148 } 149 | Popular Tags |