📄 categoryaction.java
字号:
package com.shop.struts.actions;
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 com.shop.biz.CategoryBean;
import com.shop.dao.common.DateUtil;
import com.shop.data.AbractBrand;
import com.shop.data.AbractCategory;
import com.shop.data.AbractProduct;
import com.shop.data.CategoryList;
import com.shop.data.CategorySecondAndThird;
import com.shop.data.Page;
import com.shop.data.Pagination;
import com.shop.globals.MessageOperate;
import com.shop.globals.PublicParameter;
import com.shop.globals.Transform;
import com.shop.interfaces.ICategory;
public class CategoryAction extends MappingDispatchAction {
private ICategory icategory;
public void setIcategory(ICategory icategory) {
this.icategory = icategory;
}
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);
if(cid==-1 || !icategory.isExit(cid, 3)){
MessageOperate.setAttribute(request, "非法操作", "操作错误", "您所访问的id:"+xcid+",为空或不存在!程序将跳转到上一页.", "");
return mapping.findForward("message");
}else{
com.shop.entity.Category category = icategory.findById(cid);
Integer secondid = icategory.getParentID(cid);
Integer rootid = icategory.getParentID(secondid);
List<AbractCategory> secondCategory = icategory.listSecondCategoryByTop(rootid);
List<CategorySecondAndThird> ocategorys = new Vector<CategorySecondAndThird>();
//2级分类和3级分类
if(secondCategory!=null)
for(AbractCategory c:secondCategory)
ocategorys.add(new CategorySecondAndThird(c,icategory.listHotThirdCategoryBySecond(c.getId())));
//所有商品数
// Long totalpronum = thirdCategoryDAO.listProductNumByCategory(cid);
//所有品牌
List<AbractBrand> brands = icategory.listAllBrandWithProductCount(cid);
//每周销售排行
List<AbractProduct> saleproducts = icategory.listHotSaleProduct(cid, 10, DateUtil.formatDateSub(new Date(), 7), DateUtil.formatDateSub(new Date(), 0));
//每周关注排行
List<AbractProduct> viewproducts = icategory.listHotAttentionProduct(cid, 10, DateUtil.formatDateSub(new Date(), 7), DateUtil.formatDateSub(new Date(), 0));
//最近浏览记录
List<AbractProduct> lastviews = icategory.listUserLastViewProduct(request.getCookies());
//全部商品
List<AbractProduct> products = icategory.listAllProductByCategory(cid, everypage, page, bid, order);
//分页信息
String requestpage = "category.htm?cid="+cid+(order==null?"":"&order="+order)+(bid.equals(-1)?"":"&bid="+bid)+"&page=";
Long pamount = icategory.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> > <a href='rootcategory.htm?id="+rootid+"' id='aTabCategory'>"+
icategory.findById(rootid).getCname()
+"</a> > <span id='aRootCategory'>"+
icategory.findById(secondid).getCname()
+"</span> > <b><span id='spanCategory'>"+
icategory.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", "所有分类");
//一级分类
List<CategoryList> allcategorys = new Vector<CategoryList>();
/////////////////////////////////////////////////////////////////
List<AbractCategory> acs=icategory.listAllTopCategory();
for(AbractCategory ac:acs){
CategoryList cl=new CategoryList(ac);
new com.shop.struts.actions.CategoryAction().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;
List<CategoryList> list = new Vector<CategoryList>();
try {
for(AbractCategory ac:icategory.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 + -