⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 category.java

📁 网上购物系统
💻 JAVA
字号:
/**
  * @(#)tarena.action.Category.java  2008-11-3  
  * Copy Right Information	: Tarena
  * Project					: xindou
  * JDK version used		: jdk1.6.4
  * Comments				: 此处输入简单类说明
  * Version					: 1.0
  * Sr	Date		Modified By		Why & What is modified
  * 1.	2008-11-3 	小猪     		新建
  * 2.  2008-11-18  陈达				修改
  **/
package tarena.action;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import tarena.dao.CategoryDAO;
import tarena.dao.pages.CategoryListDAO;
import tarena.dao.pages.DateUtil;
import tarena.dao.pages.RootCategoryDAO;
import tarena.dao.pages.ThirdCategoryDAO;
import tarena.data.AbractBrand;
import tarena.data.AbractCategory;
import tarena.data.AbractProduct;
import tarena.data.CategoryList;
import tarena.data.CategorySecondAndThird;
import tarena.data.Page;
import tarena.data.Pagination;
import tarena.global.MessageOperate;
import tarena.global.PublicParameter;
import tarena.global.Transform;

 /**
 * 3级分类商品页。
 * 2008-11-3
 * @author		达内科技[Tarena Training Group]
 * @version	1.0
 * @since		JDK1.6(建议) 
 */
public class Category extends MappingDispatchAction {
	
	
	public ActionForward category(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		//此处记得保存rootid
		String xcid = request.getParameter("cid");
		String order = request.getParameter("order");
		Integer bid = Transform.StringToInteger(request.getParameter("bid"));
		Integer page=1;
		int everypage = PublicParameter.PRODUCT_NUMBER_EVERY_PAGE;
		page = Transform.StringToInteger(request.getParameter("page"));
		if(page == -1){
			page = 1;
		}		
		Integer cid = Transform.StringToInteger(xcid);
		CategoryDAO categoryDAO = new CategoryDAO();
		if(cid==-1 || !categoryDAO.isExit(cid, 3)){
			MessageOperate.setAttribute(request, "非法操作", "操作错误", "您所访问的id:"+xcid+",为空或不存在!程序将跳转到上一页.", "");
			return mapping.findForward("message");
		}else{
			tarena.entity.Category category = categoryDAO.findById(cid);
			Integer secondid = categoryDAO.getParentID(cid);
			Integer rootid = categoryDAO.getParentID(secondid);
			ThirdCategoryDAO thirdCategoryDAO = new ThirdCategoryDAO();
			
			
			RootCategoryDAO rootCategoryDAO = new RootCategoryDAO();
			List<AbractCategory> secondCategory = rootCategoryDAO.listSecondCategoryByTop(rootid);
			List<CategorySecondAndThird> ocategorys = new Vector<CategorySecondAndThird>();
			//2级分类和3级分类
			if(secondCategory!=null)
				for(AbractCategory c:secondCategory)
					ocategorys.add(new CategorySecondAndThird(c,rootCategoryDAO.listHotThirdCategoryBySecond(c.getId())));
			//所有商品数
			Long totalpronum = thirdCategoryDAO.listProductNumByCategory(cid);
			//所有品牌
			List<AbractBrand> brands = thirdCategoryDAO.listAllBrandWithProductCount(cid);
			//每周销售排行
			//List<AbractProduct> saleproducts = thirdCategoryDAO.listHotSaleProduct(cid, 10, DateUtil.formatDateSub(new Date(), 7), DateUtil.formatDateSub(new Date(), 0));
			List<AbractProduct> saleproducts = thirdCategoryDAO.listHotAttentionProduct(cid, 10, DateUtil.formatDateSub(new Date(), 20), DateUtil.formatDateSub(new Date(), 0));
			//每周关注排行
			List<AbractProduct> viewproducts = thirdCategoryDAO.listHotAttentionProduct(cid, 10, DateUtil.formatDateSub(new Date(), 7), DateUtil.formatDateSub(new Date(), 0));
			//最近浏览记录
			List<AbractProduct> lastviews = thirdCategoryDAO.listUserLastViewProduct(request.getCookies());
			//全部商品
			List<AbractProduct> products = thirdCategoryDAO.listAllProductByCategory(cid, everypage, page, bid, order);
			//分页信息
			String requestpage = "category.htm?cid="+cid+(order==null?"":"&order="+order)+(bid.equals(-1)?"":"&bid="+bid)+"&page=";
			long pamount = thirdCategoryDAO.listProductNumByCategoryAndBrand(cid, bid);
			//System.out.println(pamount+","+bid);
			int endpage = (int)(pamount%everypage==0?pamount/everypage:pamount/everypage+1);
			Pagination pagination = new Pagination();
			pagination.setFirst(new Page("首页",page.equals(1)?null:requestpage+1));
			pagination.setPrevious(new Page("上一页",page.equals(1)?null:requestpage+(page-1)));
			List<Page> content = new LinkedList<Page>();
			for(int i=1;i<=endpage;i++){
				if(page.equals(i)){
					content.add(new Page(""+i,null));
				}else{					
					content.add(new Page(""+i,requestpage+i));
				}
			}
			pagination.setContent(content);
			pagination.setNext(new Page("下一页",page.equals(endpage)?null:requestpage+(page+1)));
			pagination.setLast(new Page("末页",page.equals(endpage)?null:requestpage+endpage));
			
			
			//请求中设置属性
			request.setAttribute("title", category.getCname());
			request.setAttribute("rootid", rootid);
			request.setAttribute("address", "<a href='default.htm'>首页</a>&nbsp;> &nbsp;<a href='rootcategory.htm?id="+rootid+"' id='aTabCategory'>"+
					categoryDAO.findById(rootid).getCname()
					+"</a>&nbsp;>&nbsp;<span id='aRootCategory'>"+
					categoryDAO.findById(secondid).getCname()
					+"</span>&nbsp;>&nbsp;<b><span id='spanCategory'>"+
					categoryDAO.findById(cid).getCname()
					+"</span></b>");
			request.setAttribute("scid", secondid);
			request.setAttribute("ocategorys", ocategorys);
			request.setAttribute("totalpronum", totalpronum);
			request.setAttribute("brands", brands);
			request.setAttribute("saleproducts", saleproducts);
			request.setAttribute("viewproducts", viewproducts);
			request.setAttribute("lastviews", lastviews);
			request.setAttribute("products", products);
			request.setAttribute("pagination", pagination);
		}
		return mapping.findForward("category");
	}
	
	public ActionForward categorylist(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setAttribute("title", "所有分类");
		//一级分类
		CategoryListDAO dao = new CategoryListDAO();
		List<CategoryList> allcategorys = new Vector<CategoryList>();
		/////////////////////////////////////////////////////////////////
		List<AbractCategory> acs=dao.listAllTopCategory();
		for(AbractCategory ac:acs){
			CategoryList cl=new CategoryList(ac);
			new tarena.action.Category().findChild(cl);
			allcategorys.add(cl);
		}
		/////////////////////////////////////////////////////////////////
		request.setAttribute("allcategorys", allcategorys);
		return mapping.findForward("categorylist");
	}
	
		
	private void findChild(CategoryList category){
		if(category.getChildcategory()!=null&&category.getChildcategory().size()!=0) return;
		CategoryListDAO dao = new CategoryListDAO();
		List<CategoryList> list = new Vector<CategoryList>();		
		try {
			for(AbractCategory ac:dao.listAllChildCategory(category.getRootcategory().getId())){
				list.add(new CategoryList(ac));
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		category.setChildcategory(list);
		
		if(category.getChildcategory()==null||category.getChildcategory().size()==0) return;
		for(CategoryList child:category.getChildcategory()){
			findChild(child);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -