CShop Project 08: 開發商品分類的查詢和展示
阿新 • • 發佈:2020-08-24
效果
0. Model層中: Type.java
1. TypeDao.java
public List<Type> selectAll() throws SQLException{ QueryRunner r = new QueryRunner(DBUtil.getDataSource()); String sql = "select * from type"; return r.query(sql, new BeanListHandler<Type>(Type.class)); }
2. TypeService.java
public class TypeService { public TypeDao tDao = new TypeDao(); public List<Type> selectAll(){ List<Type> list = null; try { list = tDao.selectAll(); } catch (SQLException e) { e.printStackTrace(); }return list; } }
3. ApplicationListener.java
@WebListener public class ApplicationListener implements ServletContextListener { private TypeService tService = new TypeService(); public void contextDestroyed(ServletContextEvent arg0) { } public void contextInitialized(ServletContextEvent arg0) { List<Type> list = tService.selectAll(); arg0.getServletContext().setAttribute("typeList", list); } }
4. header.jsp
<h4>商品分類</h4> <ul class="multi-column-dropdown"> <li><a class="list" href="goods.action?typeid=5">全部系列</a></li> <c:forEach items="${typeList }" var="t"> <li><a class="list" href="goods.action?typeid=5">${t.name }</a></li> </c:forEach> </ul>