(九)分类展示上
最初的设计
点击首页的时候,查询分类信息
步骤分析:
1.创建分类表
2.在indexservlet上查询分类信息
3.调用CategoryService.findAll() 返回值是:list
创建分类表
见http://www.cnblogs.com/Michael2397/p/7633263.html
创建分类实体
package com.louis.domain; public class Category { private String cid; private String cname; public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } }
创建service
/* * 查询所有的分类 * */ public List<Category> findAll() throws Exception { CategoryDao categoryDao = new CategoryDaoImpl(); return categoryDao.findAll(); }
创建dao
@Override public List<Category> findAll() throws Exception { QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource()); String sql="select * from category"; return qr.query(sql, new BeanListHandler<>(Category.class)); }
IndexServlet
/** * 和首页相关的servlet */ public class IndexServlet extends BaseServlet { public String index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1、调用categoryservice查询所有的分类,返回list CategoryService categoryService = new CategoryServiceImpl(); List<Category> clist =null; try { clist = categoryService.findAll(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //2、将返回值放入request域中 request.setAttribute("clist", clist); // 去数据库中查询最新商品和热门商品,将他们放入request域中请求转发 return "/jsp/index.jsp"; }
前端
效果
posted on 2017-10-09 09:51 Michael2397 阅读(...) 评论(...) 编辑 收藏