CShop Project 08: 开发商品分类的查询和展示

效果

 

 

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>

 

posted @ 2020-08-24 11:54  Jasper2003  阅读(184)  评论(0编辑  收藏  举报