我有一个类产品,它具有属性名称,描述和价格。
我打算在我的项目的应用范围内填充产品。我该如何实现这一目标?
其次,在填充产品时,我希望能够在JSP页面的表中显示每个产品。每个产品都有自己的表,列出其名称,描述和价格以及添加到购物车按钮
答案 0 :(得分:3)
我打算在我的项目的应用范围内填充产品。我该如何实现这一目标?
所以你想在webapp的生命周期中填充一次?使用ServletContextListener
。
@WebListener
public class StartupListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
List<Product> products = loadItSomehow();
event.getServletContext().setAttribute("products", products);
}
// ...
}
这样,产品将在每个servlet中可用
List<Product> products = (List<Product>) getServletContext().getAttribute("products");
并在每个JSP中
${products}
其次,在填充产品时,我希望能够在JSP页面的表中显示每个产品。每个产品都有自己的表,列出其名称,描述和价格以及添加到购物车按钮
所以你想对产品进行分类?有一个List<Category>
然后Category
类有List<Product>
,或者使用Map<String, List<Product>>
,其中键是类别名称。
至于如何展示它,已经在other question。
中回答了问题