巴巴运动网学习笔记(86-90)
1.显示大类别下的所有产品
a.查找某个类别下面的所有子类及子类的子类
View Code
1 @SuppressWarnings("unchecked") 2 @Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)//该方法为只读,不需要事务支持 3 public List<ProductType> getChildsByParentId(Integer parentId){ 4 Query query = entityManager.createQuery("select t from ProductType t where t.parent.id = ?1"); 5 query.setParameter(1, (int)parentId); 6 return query.getResultList(); 7 } 8 9 public List<ProductType> getAllChildsByParentId(Integer parentId){ 10 List<ProductType> productTypeIds = new ArrayList<ProductType>(); 11 Queue<ProductType> temp = new LinkedList<ProductType>(); 12 ProductType p = find(ProductType.class, (int)parentId); 13 while(p!=null){ 14 productTypeIds.add(p); 15 List<ProductType> childIds = getChildsByParentId(p.getId()); 16 for(ProductType i : childIds){ 17 temp.offer(i); 18 } 19 p = temp.poll(); 20 } 21 return productTypeIds; 22 }
b.获取某个类别下面的所有产品
View Code
1 /** 2 * 根据产品类型id查找该产品类型及子类型下的所有产品的品牌 3 */ 4 @SuppressWarnings("unchecked") 5 @Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED) 6 public List<Brand> getBrandsByProTyId(int proTypeID){ 7 List<ProductType> productTypes = productTypeService.getAllChildsByParentId(proTypeID); 8 //组拼根据ids查询产品的语句 9 StringBuffer jpql2 = new StringBuffer(""); 10 for(int i=0;i<productTypes.size();i++){ 11 jpql2.append("?").append(i+1).append(","); 12 } 13 jpql2.deleteCharAt(jpql2.length()-1); 14 StringBuffer jpql = new StringBuffer("select distinct b from Brand b where b.id in (select p.brand.id from Product p where p.productType.id in ("+jpql2+"))"); 15 Query query = entityManager.createQuery(jpql.toString()); 16 //为其设置参数 17 for(int i=0;i<productTypes.size();i++){ 18 query.setParameter(i+1, productTypes.get(i).getId()); 19 } 20 return query.getResultList(); 21 }
2.在前台页面中显示导航菜单和子类别
View Code
1 //设置导航数据 2 Stack<ProductType> navigation = new Stack<ProductType>(); 3 ProductType parent = productService.find(ProductType.class,productForm.getProductTypeId()); 4 while(parent!=null){ 5 navigation.push(parent); 6 parent = parent.getParent(); 7 } 8 List<ProductType> navigationList = new ArrayList<ProductType>(); 9 while(!navigation.empty()){ 10 navigationList.add(navigation.pop()); 11 }
View Code
1 <div id="position">您现在的位置:>> <a href="/" name="linkHome">巴巴运动网</a> 2 <c:forEach items="${navigation}" var="nv" varStatus="state"> 3 <c:choose> 4 <c:when test="${state.count==fn:length(navigation)}">>><em>${nv.name }</em></c:when> 5 <c:otherwise>>><a href="<html:rewrite action='/product/list'/>?productTypeId=${nv.id }">${nv.name }</a></c:otherwise> 6 </c:choose> 7 </c:forEach> 8 (${pagingBean.totalRecords }) 9 </div>
View Code
1 <div class="browse_t">${productType.name}</div> 2 3 <h2><span class="gray">浏览下级分类</span></h2> 4 <ul> 5 <c:forEach items="${productType.child}" var="proType" > 6 <li class='bj_blue'><a href="<html:rewrite action='/product/list'/>?productTypeId=${proType.id }">${proType.name }</a></li> 7 </c:forEach> 8 </ul> 9 </div>
3.在前台页面中显示最畅销产品的技术分析
a.某一个产品类别下面的
b.后台推荐的
c.按照销量进行排序
d.使用ajax技术,进行异步显示
4.使用ajax技术展示最畅销产品
a.获取最畅销产品的业务方法
View Code
1 /** 2 * 查找某个类别下面的最畅销的商品 3 * @param productTypeId 类别id 4 * @param maxCount 查找个数 5 * @return 6 */ 7 @SuppressWarnings("unchecked") 8 public List<Product> getTopsellProduct(int productTypeId,int maxCount){ 9 List<ProductType> productTypes = productTypeService.getAllChildsByParentId(productTypeId); 10 //组拼根据ids查询产品的语句 11 StringBuffer jpql2 = new StringBuffer(""); 12 for(int i=0;i<productTypes.size();i++){ 13 jpql2.append("?").append(i+2).append(","); 14 } 15 jpql2.deleteCharAt(jpql2.length()-1); 16 StringBuffer jpql = new StringBuffer("select p from Product p where p.commend = ?1 and p.productType.id in ("+jpql2+") order by sellCount desc"); 17 Query query = entityManager.createQuery(jpql.toString()); 18 //为其设置参数 19 query.setParameter(1, true); 20 for(int i=0;i<productTypes.size();i++){ 21 query.setParameter(i+2, productTypes.get(i).getId()); 22 } 23 query.setFirstResult(0).setMaxResults(maxCount); 24 return query.getResultList(); 25 }
b.处理请求的manageraction
View Code
1 @Controller("/product/manager") 2 public class FrontProductManagerAction extends DispatchAction { 3 @Resource(name="productServiceImpl") 4 ProductService productService; 5 public ActionForward getTopsell(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) 6 throws Exception { 7 FrontProductForm productForm = (FrontProductForm)form; 8 request.setAttribute("topsell", productService.getTopsellProduct(productForm.getProductTypeId(), 10)); 9 return mapping.findForward("list"); 10 } 11 }
c.页面的代码
View Code
1 <SCRIPT LANGUAGE="JavaScript"> 2 <!-- 3 function getTopSell(typeid){ 4 var salespromotion = document.getElementById('salespromotion'); 5 if(salespromotion && typeid!=""){ 6 salespromotion.innerHTML= "数据正在加载..."; 7 send_request(function(value){salespromotion.innerHTML=value}, "<html:rewrite action='/product/manager'/>?method=getTopsell&productTypeId="+ typeid, true); 8 } 9 } 10 function pageInit(){ 11 getTopSell("${productType.id}"); 12 } 13 //--> 14 </SCRIPT>
View Code
1 <body class="ProducTypeHome2" onload="JavaScript:pageInit()">
View Code
1 <DIV class="lanmu_font">最畅销户外用品</DIV> 2 <DIV style="PADDING-LEFT: 10px; COLOR: #333333" id="salespromotion">
5.修正按品牌和性别过滤的一些bug