SSH网上商城三

现在我们要实现下面的需求:

当用户点击左侧二级菜单选项的时候,在右侧要显示对应的该二级菜单项下面存在哪些商品,例如点击潮流女装,要在右侧分页显示该潮流女装下对应哪些商品

1、要分页显示 首先要获得该二级菜单下对应商品的总数

2、分页查询和显示该二级菜单下对应商品的数目

我们首先在productList.jsp

    <s:iterator var="cs" value="#c.categorySeconds">
                                    <dd>
                                        <a href="${ pageContext.request.contextPath }/product_findByCsid.action?csid=<s:property value="#cs.csid"/>&page=1"><s:property value="#cs.csname"/></a>
                                    </dd>
                                </s:iterator>

此处就是表示当前用户点击了某个二级菜单选项  需要传递当前二级菜单的csid和当前默认的page

传递到product_findByCsid.action传递到ProductAction的findByCsid这个方法中进行处理,ProductAction中需要定义一个成员变量csid接受传递过来的二级菜单选项的id值,也需要定义一个成员变量page接受page的值

我们来看对应的代码

package cn.itcast.shop.product.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import cn.itcast.shop.category.beans.Category;
import cn.itcast.shop.category.service.CategoryService;
import cn.itcast.shop.product.beans.Product;
import cn.itcast.shop.product.service.ProductService;
import cn.itcast.shop.utils.PageBean;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class ProductAction extends ActionSupport  implements ModelDriven<Product>{

    private ProductService productService;
    
    private CategoryService categoryService;
    
    // 接收分类cid
    private Integer cid;
    
    // 接收当前页数:
        private int page;
        
        // 接收二级分类id
        private Integer csid;
        
    
    public Integer getCsid() {
            return csid;
        }
        public void setCsid(Integer csid) {
            this.csid = csid;
        }
    public int getPage() {
            return page;
        }
        public void setPage(int page) {
            this.page = page;
        }
    public Integer getCid() {
        return cid;
    }
    public void setCid(Integer cid) {
        this.cid = cid;
    }
    public CategoryService getCategoryService() {
        return categoryService;
    }
    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }
    public ProductService getProductService() {
        return productService;
    }
    public void setProductService(ProductService productService) {
        this.productService = productService;
    }
    public Product getProduct() {
        return product;
    }
    public void setProduct(Product product) {
        this.product = product;
    }
    private Product product = new Product();
    @Override
    public Product getModel() {
        // TODO Auto-generated method stub
        return product;
    }
    
    // 根据商品的ID进行查询商品:执行方法:
        public String findByPid() throws Exception{
            // 调用Service的方法完成查询.
            product = productService.findByPid(product.getPid());
            return "findByPid";
        }
        
        // 根据分类的id查询商品:
        public String findByCid() {
            List<Category> cList = categoryService.findAll();
            ActionContext.getContext().getValueStack().set("cList", cList);
            PageBean<Product> pageBean = productService.findByPageCid(cid, page);// 根据一级分类查询商品,带分页查询
            // 将PageBean存入到值栈中:
            ActionContext.getContext().getValueStack().set("pageBean", pageBean);
            return "findByCid";
        }

        // 根据二级分类id查询商品:
        public String findByCsid() {
            // 根据二级分类查询商品
            PageBean<Product> pageBean = productService.findByPageCsid(csid, page);
            // 将PageBean存入到值栈中:
            ActionContext.getContext().getValueStack().set("pageBean", pageBean);
            return "findByCsid";
        }

}

我们来看看对应的业务类方法

package cn.itcast.shop.product.service;

import java.util.List;

import cn.itcast.shop.product.beans.Product;
import cn.itcast.shop.product.dao.ProductDao;
import cn.itcast.shop.utils.PageBean;

public class ProductService {
    private ProductDao productDao;

    public ProductDao getProductDao() {
        return productDao;
    }

    public void setProductDao(ProductDao productDao) {
        this.productDao = productDao;
    }

    public List<Product> findHot() {
        // TODO Auto-generated method stub
        return productDao.findHot();
    }
    public List<Product> findNew(){
        return productDao.findNew();
    }
    public Product findByPid(Integer pid) {
        return productDao.findByPid(pid);
    }

    
    // 根据一级分类的cid带有分页查询商品
        public PageBean<Product> findByPageCid(Integer cid, int page) {
            PageBean<Product> pageBean = new PageBean<Product>();
            // 设置当前页数:
            pageBean.setPage(page);
            // 设置每页显示记录数:
            int limit = 8;
            pageBean.setLimit(limit);
            // 设置总记录数:
            int totalCount = 0;
            totalCount = productDao.findCountCid(cid);
            pageBean.setTotalCount(totalCount);
            // 设置总页数:
            int totalPage = 0;
            // Math.ceil(totalCount / limit);
            if (totalCount % limit == 0) {
                totalPage = totalCount / limit;
            } else {
                totalPage = totalCount / limit + 1;
            }
            pageBean.setTotalPage(totalPage);
            // 每页显示的数据集合:
            // 从哪开始:
            int begin = (page - 1) * limit;
            List<Product> list = productDao.findByPageCid(cid, begin, limit);
            pageBean.setList(list);
            return pageBean;
        }

        // 根据二级分类查询商品信息
        public PageBean<Product> findByPageCsid(Integer csid, int page) {
            PageBean<Product> pageBean = new PageBean<Product>();
            // 设置当前页数:
            pageBean.setPage(page);
            // 设置每页显示记录数:
            int limit = 8;
            pageBean.setLimit(limit);
            // 设置总记录数:
            int totalCount = 0;
            totalCount = productDao.findCountCsid(csid);
            pageBean.setTotalCount(totalCount);
            // 设置总页数:
            int totalPage = 0;
            // Math.ceil(totalCount / limit);
            if (totalCount % limit == 0) {
                totalPage = totalCount / limit;
            } else {
                totalPage = totalCount / limit + 1;
            }
            pageBean.setTotalPage(totalPage);
            // 每页显示的数据集合:
            // 从哪开始:
            int begin = (page - 1) * limit;
            List<Product> list = productDao.findByPageCsid(csid, begin, limit);
            pageBean.setList(list);
            return pageBean;
        }
    

}

我们来看看对应的dao数据库层的方法

package cn.itcast.shop.product.dao;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.itcast.shop.product.beans.Product;
import cn.itcast.shop.utils.PageHibernateCallback;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;



public class ProductDao extends HibernateDaoSupport {

    // 首页上热门商品查询
        public List<Product> findHot() {
            // 使用离线条件查询.
            DetachedCriteria criteria = DetachedCriteria.forClass(Product.class);
            // 查询热门的商品,条件就是is_host = 1
            criteria.add(Restrictions.eq("is_hot", 1));
            // 倒序排序输出:
            criteria.addOrder(Order.desc("pdate"));
            // 执行查询:
            List<Product> list = this.getHibernateTemplate().findByCriteria(
                    criteria, 0, 10);
            return list;
        }
        

        // 首页上最新商品的查询
        public List<Product> findNew() {
            // 使用离线条件查询:
            DetachedCriteria criteria = DetachedCriteria.forClass(Product.class);
            // 按日期进行倒序排序:
            criteria.addOrder(Order.desc("pdate"));
            // 执行查询:
            List<Product> list = this.getHibernateTemplate().findByCriteria(criteria, 0, 10);
            return list;
        }
        
        // 根据商品ID查询商品
        public Product findByPid(Integer pid) {
            return (Product) this.getHibernateTemplate().get(Product.class, pid);
        }

        //获得对应的一级分类下的所有的商品总的记录数
        public int findCountCid(Integer cid) {
            // TODO Auto-generated method stub
            String hql = "select count(*) from Product p where p.categorySecond.category.cid = ?";
            List<Long> list = this.getHibernateTemplate().find(hql,cid);
            if(list != null && list.size() > 0){
                return list.get(0).intValue();
            }
            return 0;
        }
        
    

        // 根据分类id查询商品的集合
        public List<Product> findByPageCid(Integer cid, int begin, int limit) {
            // select p.* from category c,categorysecond cs,product p where c.cid = cs.cid and cs.csid = p.csid and c.cid = 2
            // select p from Category c,CategorySecond cs,Product p where c.cid = cs.category.cid and cs.csid = p.categorySecond.csid and c.cid = ?
            String hql = "select p from Product p join p.categorySecond cs join cs.category c where c.cid = ?";
            // 分页另一种写法:
            List<Product> list = (List<Product>) this.getHibernateTemplate().execute(new PageHibernateCallback<Product>(hql, new Object[]{cid}, begin, limit));
            if(list != null && list.size() > 0){
                return list;
            }
            return null;
            
        }

        // 根据二级分类查询商品个数
        public int findCountCsid(Integer csid) {
            String hql = "select count(*) from Product p where p.categorySecond.csid = ?";
            List<Long> list = this.getHibernateTemplate().find(hql, csid);
            if(list != null && list.size() > 0){
                return list.get(0).intValue();
            }
            return 0;
        }

        // 根据二级分类查询商品信息
        public List<Product> findByPageCsid(Integer csid, int begin, int limit) {
            String hql = "select p from Product p join p.categorySecond cs where cs.csid = ?";
            List<Product> list = (List<Product>) this.getHibernateTemplate().execute(new PageHibernateCallback<Product>(hql, new Object[]{csid}, begin, limit));
            if(list != null && list.size() > 0){
                return list;
            }
            return null;
        }
        

    

}

// 根据二级分类id查询商品:
        public String findByCsid() {
            // 根据二级分类查询商品
            PageBean<Product> pageBean = productService.findByPageCsid(csid, page);
            // 将PageBean存入到值栈中:
            ActionContext.getContext().getValueStack().set("pageBean", pageBean);
            return "findByCsid";
        }

我们将查询的二级菜单选项下的商品集合、当前的page等封装到了一个PageBean对象中存储在值栈中

    <!-- 商品模块的Action -->
        <action name="product_*" class="productAction" method="{1}">
            <result name="findByPid">/WEB-INF/jsp/product.jsp</result>
            <result name="findByCid">/WEB-INF/jsp/productList.jsp</result>
            <result name="findByCsid">/WEB-INF/jsp/productList.jsp</result>
        </action>
    </package>

这里我们是跳转到

productList.jsp

这里既可以显示一级分类下的商品,也可以显示二级分类下的商品,所以在

productList.jsp需要判断当前是显示那种情况
    <s:if test="cid != null">
            <s:if test="pageBean.page != 1">
                <a href="${ pageContext.request.contextPath }/product_findByCid.action?cid=<s:property value="cid"/>&page=1" class="firstPage">&nbsp;</a>
                <a href="${ pageContext.request.contextPath }/product_findByCid.action?cid=<s:property value="cid"/>&page=<s:property value="pageBean.page-1"/>" class="previousPage">&nbsp;</a>
            </s:if>
            
            <s:iterator var="i" begin="1" end="pageBean.totalPage">
                <s:if test="pageBean.page != #i">
                    <a href="${ pageContext.request.contextPath }/product_findByCid.action?cid=<s:property value="cid"/>&page=<s:property value="#i"/>"><s:property value="#i"/></a>
                </s:if>
                <s:else>
                    <span class="currentPage"><s:property value="#i"/></span>
                </s:else>
            </s:iterator>
            
            <s:if test="pageBean.page != pageBean.totalPage">    
                <a class="nextPage" href="${ pageContext.request.contextPath }/product_findByCid.action?cid=<s:property value="cid"/>&page=<s:property value="pageBean.page+1"/>">&nbsp;</a>
                <a class="lastPage" href="${ pageContext.request.contextPath }/product_findByCid.action?cid=<s:property value="cid"/>&page=<s:property value="pageBean.totalPage"/>">&nbsp;</a>
            </s:if>
        </s:if>    
        <s:if test="csid != null">
            <s:if test="pageBean.page != 1">
                <a href="${ pageContext.request.contextPath }/product_findByCsid.action?csid=<s:property value="csid"/>&page=1" class="firstPage">&nbsp;</a>
                <a href="${ pageContext.request.contextPath }/product_findByCsid.action?csid=<s:property value="csid"/>&page=<s:property value="pageBean.page-1"/>" class="previousPage">&nbsp;</a>
            </s:if>
            
            <s:iterator var="i" begin="1" end="pageBean.totalPage">
                <s:if test="pageBean.page != #i">
                    <a href="${ pageContext.request.contextPath }/product_findByCsid.action?csid=<s:property value="csid"/>&page=<s:property value="#i"/>"><s:property value="#i"/></a>
                </s:if>
                <s:else>
                    <span class="currentPage"><s:property value="#i"/></span>
                </s:else>
            </s:iterator>
            
            <s:if test="pageBean.page != pageBean.totalPage">    
                <a class="nextPage" href="${ pageContext.request.contextPath }/product_findByCsid.action?csid=<s:property value="csid"/>&page=<s:property value="pageBean.page+1"/>">&nbsp;</a>
                <a class="lastPage" href="${ pageContext.request.contextPath }/product_findByCsid.action?csid=<s:property value="csid"/>&page=<s:property value="pageBean.totalPage"/>">&nbsp;</a>
            </s:if>
        </s:if>    
<s:if test="cid != null">表示就是当前一级分类的cid不等于null,就显示一级分类下的商品进行分页显示
整个
productList.jsp的代码如下所示:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- saved from url=(0048)http://localhost:8080/mango/product/list/1.jhtml -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>传智网上商城</title>
<link href="${pageContext.request.contextPath}/css/common.css" rel="stylesheet" type="text/css"/>
<link href="${pageContext.request.contextPath}/css/product.css" rel="stylesheet" type="text/css"/>

</head>
<body>
<div class="container header">
    <div class="span5">
        <div class="logo">
            <a href="http://localhost:8080/mango/">
                <img src="${pageContext.request.contextPath}/image/r___________renleipic_01/logo.gif" alt="传智播客">
            </a>
        </div>
    </div>
    <div class="span9">
<div class="headerAd">
                    <img src="${pageContext.request.contextPath}/image/header.jpg" width="320" height="50" alt="正品保障" title="正品保障">
</div>    </div>
    
    <%@ include file="menu.jsp" %>
    
</div>    
<div class="container productList">
        <div class="span6">
            <div class="hotProductCategory">
                <s:iterator var="c" value="#session.cList">
                        <dl>
                            <dt>
                                <a href="${pageContext.request.contextPath}/product_findByCid.action?cid=<s:property value="#c.cid"/>&page=1"><s:property value="#c.cname"/></a>
                            </dt>
                                <s:iterator var="cs" value="#c.categorySeconds">
                                    <dd>
                                        <a href="${ pageContext.request.contextPath }/product_findByCsid.action?csid=<s:property value="#cs.csid"/>&page=1"><s:property value="#cs.csname"/></a>
                                    </dd>
                                </s:iterator>    
                        </dl>
                </s:iterator>        
            </div>
        </div>
        <div class="span18 last">
            
            <form id="productForm" action="${pageContext.request.contextPath}/image/蔬菜 - Powered By Mango Team.htm" method="get">
                    
                <div id="result" class="result table clearfix">
                        <ul>
                            <s:iterator var="p" value="pageBean.list">
                                <li>
                                        <a href="${ pageContext.request.contextPath }/product_findByPid.action?pid=<s:property value="#p.pid"/>">
                                            <img src="${pageContext.request.contextPath}/<s:property value="#p.image"/>" width="170" height="170"  style="display: inline-block;">
                                               
                                            <span style='color:green'>
                                             <s:property value="#p.pname"/>
                                            </span>
                                             
                                            <span class="price">
                                                商城价: ¥<s:property value="#p.shop_price"/>
                                            </span>
                                             
                                        </a>
                                </li>
                            </s:iterator>    
                                
                        </ul>
                </div>
    <div class="pagination">
            <span><s:property value="pageBean.page"/>/<s:property value="pageBean.totalPage"/></span>
        <s:if test="cid != null">
            <s:if test="pageBean.page != 1">
                <a href="${ pageContext.request.contextPath }/product_findByCid.action?cid=<s:property value="cid"/>&page=1" class="firstPage">&nbsp;</a>
                <a href="${ pageContext.request.contextPath }/product_findByCid.action?cid=<s:property value="cid"/>&page=<s:property value="pageBean.page-1"/>" class="previousPage">&nbsp;</a>
            </s:if>
            
            <s:iterator var="i" begin="1" end="pageBean.totalPage">
                <s:if test="pageBean.page != #i">
                    <a href="${ pageContext.request.contextPath }/product_findByCid.action?cid=<s:property value="cid"/>&page=<s:property value="#i"/>"><s:property value="#i"/></a>
                </s:if>
                <s:else>
                    <span class="currentPage"><s:property value="#i"/></span>
                </s:else>
            </s:iterator>
            
            <s:if test="pageBean.page != pageBean.totalPage">    
                <a class="nextPage" href="${ pageContext.request.contextPath }/product_findByCid.action?cid=<s:property value="cid"/>&page=<s:property value="pageBean.page+1"/>">&nbsp;</a>
                <a class="lastPage" href="${ pageContext.request.contextPath }/product_findByCid.action?cid=<s:property value="cid"/>&page=<s:property value="pageBean.totalPage"/>">&nbsp;</a>
            </s:if>
        </s:if>    
        <s:if test="csid != null">
            <s:if test="pageBean.page != 1">
                <a href="${ pageContext.request.contextPath }/product_findByCsid.action?csid=<s:property value="csid"/>&page=1" class="firstPage">&nbsp;</a>
                <a href="${ pageContext.request.contextPath }/product_findByCsid.action?csid=<s:property value="csid"/>&page=<s:property value="pageBean.page-1"/>" class="previousPage">&nbsp;</a>
            </s:if>
            
            <s:iterator var="i" begin="1" end="pageBean.totalPage">
                <s:if test="pageBean.page != #i">
                    <a href="${ pageContext.request.contextPath }/product_findByCsid.action?csid=<s:property value="csid"/>&page=<s:property value="#i"/>"><s:property value="#i"/></a>
                </s:if>
                <s:else>
                    <span class="currentPage"><s:property value="#i"/></span>
                </s:else>
            </s:iterator>
            
            <s:if test="pageBean.page != pageBean.totalPage">    
                <a class="nextPage" href="${ pageContext.request.contextPath }/product_findByCsid.action?csid=<s:property value="csid"/>&page=<s:property value="pageBean.page+1"/>">&nbsp;</a>
                <a class="lastPage" href="${ pageContext.request.contextPath }/product_findByCsid.action?csid=<s:property value="csid"/>&page=<s:property value="pageBean.totalPage"/>">&nbsp;</a>
            </s:if>
        </s:if>    
    </div>
            </form>
        </div>
    </div>
<div class="container footer">
    <div class="span24">
        <div class="footerAd">
                    <img src="${pageContext.request.contextPath}/image/footer.jpg" width="950" height="52" alt="我们的优势" title="我们的优势">
</div>    </div>
    <div class="span24">
        <ul class="bottomNav">
                    <li>
                        <a >关于我们</a>
                        |
                    </li>
                    <li>
                        <a>联系我们</a>
                        |
                    </li>
                    <li>
                        <a >诚聘英才</a>
                        |
                    </li>
                    <li>
                        <a >法律声明</a>
                        |
                    </li>
                    <li>
                        <a>友情链接</a>
                        |
                    </li>
                    <li>
                        <a target="_blank">支付方式</a>
                        |
                    </li>
                    <li>
                        <a  target="_blank">配送方式</a>
                        |
                    </li>
                    <li>
                        <a >官网</a>
                        |
                    </li>
                    <li>
                        <a >论坛</a>
                        
                    </li>
        </ul>
    </div>
    <div class="span24">
        <div class="copyright">Copyright©2005-2015 网上商城 版权所有</div>
    </div>
</div>
</body></html>
原文地址:https://www.cnblogs.com/kebibuluan/p/8333853.html