栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

前台的开发(二)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

前台的开发(二)

(二)商品分类的查询和展示

创建监听器

创建service和dao层的查询方法
  1. 将商品分类的数据查询
public class TypeDao {

	public List selectAll() throws SQLException {
		//取得数据库连接
		QueryRunner r = new QueryRunner(DBUtils.getDataSource());
		String sql = "select * from type";
		return r.query(sql, new BeanListHandler(Type.class));
	}
}
  1. 将查询结果返回并由类 TypeService 中的方法调用返回
public class TypeService {

	private TypeDao tDao = new TypeDao();
	
	public List selectAll(){
		List list = null;
		try {
			list = tDao.selectAll();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
}
  1. ApplicationListener监听器在项目启动时,开始查询得到数据,将商品分类的数据显示
	//创建TypeService对象
	private TypeService tService = new TypeService();
	
	public void contextInitialized(ServletContextEvent arg0)  { 
         // TODO Auto-generated method stub
    	List list = tService.selectAll();
    	arg0.getServletContext().setAttribute("typeList", list);
    }
创建分页的数据模型
//分页的数据模型
public class Page {

	private int pageNumber;//当前页数
	private int pageSize;//每页显示的数据个数
	private int totalCount;//商品的总数
	private int totalPage;//总页数
	
	private List list;
 

根据 每页商品的个数 和 商品总数 计算 总页数 的方法

public void setPageSizeAndTotalCount(int pageSize,int totalCount) {
		this.pageSize = pageSize;
		this.totalCount = totalCount;
		
		totalPage = (int) Math.ceil((double)totalCount/pageSize);
	}
创建servlet控制商品分类的页面跳转
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		int id = 0;//id为0时跳转的是全部系列
		if(request.getParameter("id") != null) {
			id = Integer.parseInt(request.getParameter("id"));
		}
		//默认跳转的页面
		int pageNo = 1;
		if(request.getParameter("pageNo") != null) {
			pageNo = Integer.parseInt(request.getParameter("pageNo"));
		}
		Page p = gService.getGoodsPage(id, pageNo);
		request.setAttribute("p", p);
		
		//跳转到goods_list.jsp
		request.getRequestDispatcher("/goods_list.jsp").forward(request, response);
	}
商品分类中系列的商品查询
public List selectGoods(int typeId,int pageNo,int pageSize) throws SQLException{

		QueryRunner r = new QueryRunner(DBUtils.getDataSource());
		//查询全部系列
		if(typeId == 0) {
			String sql = "select * from goods limit ?,?";
			return r.query(sql, new BeanListHandler(Goods.class),(pageNo - 1)*pageSize, pageSize);
		}else {//查询其他系列
			String sql = "select * from goods where type_id = ? limit ?,?";
			return r.query(sql, new BeanListHandler(Goods.class), typeId, (pageNo - 1)*pageSize, pageSize);
		}
	}

查询商品的总数

public int getGoodsCount(int typeId) throws SQLException {
		QueryRunner r = new QueryRunner(DBUtils.getDataSource());
		String sql = "";
		if(typeId == 0) {
			sql = "select count(*) from goods";
			return r.query(sql, new ScalarHandler()).intValue();
		}else {//查询其他系列
			sql = "select count(*) from goods where type_id = ?";
			return r.query(sql, new ScalarHandler(),typeId).intValue();
		}
	}

在service层调用dao层的方法创建分页的数据模型并返回商品的页数

public Page getGoodsPage(int typeId,int pageNo) {
		Page p = new Page();
		p.setPageNumber(pageNo);
		int totalCount = 0;
		try {
			totalCount = gDao.getGoodsCount(typeId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		p.setPageSizeAndTotalCount(8, totalCount);
		
		List list = null;
		try {
			list = gDao.selectGoods(typeId, pageNo, 8);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		p.setList(list);
		
		return p;
	}

最后在jsp页面中使用 c:forEach 标签遍历结果,在商品分类中点击系列商品可以跳转到各自的页面。

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号