java前台商品分级排列_前台开发-------开发商品分类的查询和展示

初始时为静态的效果:

f805821708c3e9486b05814929b74437.png

它只是一个普通的下拉列表,不能执行任何操作,我接下来的是它能在每一个界面都能点击,并且动态的获取数据,因此需要在listener层添加一个监听类:

7c3ae053928b26cf783282137ec4d18f.png

0448fcaa0a534e77d5f147522c5f6b05.png

注意需要进行勾选这个:

fc76d35aecca57f5bf2398986e1cee89.png

数据库的类目:

b9863ff3e2039cdbb7982b26c7c50e77.png

查询类目需在dao层添加一个class,命名为:TypeDao.java

查询的SQL以及抛出异常:

package com.guiyan.dao;

import java.sql.SQLException;

import java.util.List;

import org.apache.commons.dbutils.QueryRunner;

import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.guiyan.model.Type;

import com.guiyan.utils.DBUtil;

public class TypeDao {

public List selectAll() throws SQLException {

QueryRunner r=new QueryRunner(DBUtil.getDataSource());

String sql="select * from type";

return r.query(sql, new BeanListHandler(Type.class));

}

}

在service添加一个类,命名为:TypeService.java

packagecom.guiyan.service;importjava.sql.SQLException;importjava.util.List;importcom.guiyan.dao.TypeDao;importcom.guiyan.model.Type;public classTypeService {private TypeDao tDao=newTypeDao();public ListselectAll(){

List list=null;try{

list=tDao.selectAll();

}catch(SQLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}returnlist;

}

}

为了在每一个页面都能加载到,我们在ApplicationListener.java中加入:

packagecom.guiyan.listener;importjava.util.List;importjavax.servlet.ServletContextEvent;importjavax.servlet.ServletContextListener;importjavax.servlet.annotation.WebListener;importcom.guiyan.model.Type;importcom.guiyan.service.TypeService;

@WebListenerpublic class ApplicationListener implementsServletContextListener {private TypeService tService=newTypeService();public voidcontextDestroyed(ServletContextEvent arg0) {

}public voidcontextInitialized(ServletContextEvent arg0) {

List list=tService.selectAll();

arg0.getServletContext().setAttribute("typeList", list);

}

}

展示需要在header.jsp中进行展示:

初始时:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1

2 pageEncoding="UTF-8"%>

3

4

5

6

7

header

8

9

10

11

12

13

14

15

16

17

18 Toggle navigation

19

20

21

22

23 蛋糕店

24

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值