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

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

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>

SSH网上商城三的更多相关文章

  1. SSH网上商城---用户激活

    在前面的博客中,小编主要结合SSH网上商城这个项目,简单的介绍了如何实现邮件发送的这个功能,邮件发送了,接下来就是激活了,为什么呢?现在大多网站都要通过对账号进行激活,然后才能注册成功,这是防止恶性注 ...

  2. SSH网上商城---邮件发送

    注册网站账号的时候,都需要发送激活邮件,然后让注册的用户点击激活链接方可完成注册,不过话说回来,为什么注册的时候需要发送邮件呢?为什么不注册的时候直接激活呢?一定要收一封激活帐号的邮件?网站这样做的好 ...

  3. SSH网上商城---使用ajax完成用户名是否存在异步校验

    小伙伴在上网的时候,需要下载或者观看某些视频资料,更或者是在逛淘宝的时候,我们都需要注册一个用户,当我们填写好各种信息,点击确定的时候,提示用户名已经存在,小编就想,为什么当我们填写完用户名的时候,她 ...

  4. SSH网上商城---需求分析+表关系分析

    SSH---小编初次接触的时候傻傻的以为这个跟SHE有什么关系呢?又是哪路明星歌手,后来才知道小编又土鳖了,原来SSH是这个样子滴,百度百科对她这样阐述,SSH即 Spring + Struts +H ...

  5. SSH网上商城一

    Java高级项目之SSH网上商城项目实战: 1.采用目前最主流的三大框架开发即Struts2+Spring+Hibernate框架整合开发.2.通过AJAX技术提供良好的用户体验.3.提供了邮箱激活的 ...

  6. 《SSH网上商城》-视频目录--代码可以跑起来

    本课程是2015年2月份的,就是14年底的. 课程第一天的代码-添加 jsp-api.jar   servlet-api.jar就可以跑起来,环境 JDK1.7 和tomcat8, SSH网上商城\S ...

  7. [Java]ssh网上商城总结 标签: hibernatessh 2016-05-15 21:03 1099人阅读 评论(32)

    前几日敲完了ssh网上商城,虽然现在已经敲完了整个系统,却发现自己对于ssh,了解的一点都不多,什么是struts2,什么是spring,什么是hibernate,自己都是稀里糊涂,然后看了一下后面的 ...

  8. 【SSH网上商城项目实战30】项目总结

      转自:https://blog.csdn.net/eson_15/article/details/51479994 0. 写在前面 项目基本完成了,加上这个总结,与这个项目相关的博客也写了30篇了 ...

  9. 【SSH网上商城项目实战30】项目总结(附源码下载地址)

    项目基本完成了,加上这个总结,与这个项目相关的博客也写了30篇了,积少成多,写博客的过程是固化思路的一个过程,对自己很有用,同时也能帮助别人.顺便说个题外话,在学习的过程中肯定会遇到很多异常出现,我们 ...

随机推荐

  1. (九)显示交易记录 &解决相对路径问题

    UserServlet.java package com.aff.bookstore.servlet; import java.io.IOException; import javax.servlet ...

  2. sql中partition的使用

    https://www.cnblogs.com/tfiremeteor/p/6296599.html

  3. java方式实现堆排序

    一.堆排序和堆相关概念描述 堆排序是指利用堆这种数据结构所设计的一种排序算法.堆是一个近似完全二叉树的结构,并同时满足堆的性质:即子结点的值总是小于(或者大于)它的父节点,若子结点的值总是小于它的父节 ...

  4. 面试题:我们重写一个对象的时候为什么要同时重写hashcode()和equals()方法

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 在创建的类不重写hashCode()和equals() 方法时,默认使用 java 提供的 java.l ...

  5. Spring Boot笔记(二) springboot 集成 SMTP 发送邮件

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 笔记:乘着项目迭代的间隙,把一些可复用的功能从项目中抽取出来,这是其中之一, 一.添加SMTP 及 MA ...

  6. Java实现 LeetCode 400 第N个数字

    400. 第N个数字 在无限的整数序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, -中找到第 n 个数字. 注意: n 是正数且在32为整形范围内 ( n < 231 ...

  7. Java实现 LeetCode 342 4的幂

    342. 4的幂 给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4 的幂次方. 示例 1: 输入: 16 输出: true 示例 2: 输入: 5 输出: false 进阶: 你 ...

  8. Java实现Fibonacci取余

    Description Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少. Input 多 ...

  9. Java中lang包的常用方法介绍

    JAVA API(图片来源:实验楼) 包装类 Integer包装类 方法 返回值 功能描述 byteValue() byte 以 byte 类型返回该 Integer 的值 intValue() in ...

  10. Java实现 LeetCode 4 寻找两个有序数组的中位数

    寻找两个有序数组的中位数 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 n ...