Java Web 04-7: 处理商品列表的查询
1. Goods.java
2. DBUtil.java
public class DBUtil { public static Map<String,User> userMap = new HashMap<String,User>(); public static List<Goods> goodsList = new ArrayList<Goods>(); static { addUser(new User("Jasper","123",10,"男",true)); addUser(new User("123","123",20,"女",true)); goodsList.add(new Goods("Banana","From BeiJing",100,400)); goodsList.add(new Goods("Cucumber","Very fresh",25,234)); goodsList.add(new Goods("Apple","Crispy",90,435)); goodsList.add(new Goods("Sausage","Delicious",15,124)); goodsList.add(new Goods("Clothing","Look good",30,196)); } private static void addUser(User user) { userMap.put(user.getUsername(), user); } }
3. GoodsDao.java
public class GoodsDao { public List<Goods> getAllGoods(){ return DBUtil.goodsList; } }
4. GoodsService.java
public class GoodsService { public List<Goods> selectAllGoods(){ return new GoodsDao().getAllGoods(); } }
5. GoodsListServlet.java
@WebServlet("/admin/goods_list") public class GoodsListServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { List<Goods> list = new GoodsService().selectAllGoods(); req.setAttribute("list", list); req.getRequestDispatcher("/admin/goods_list.jsp").forward(req, resp); } }