类对象和 JSP

发布于 2024-10-27 11:05:50 字数 144 浏览 2 评论 0 原文

我有一个产品类,它具有属性名称、描述和价格。

我打算在我的项目的应用范围内填充产品。我该如何实现这一目标?

其次,在填充产品后,我希望能够在 JSP 页面的表格中显示每个产品。每个产品都有自己的表格,列出其名称、描述和价格以及添加到购物车按钮

I have a class Products, which has the attributes name, description and price.

I intend to populate products in the application scope of my project. How do I go about achieving that?

Secondly, upon populating the products, I want to be able to display each product in a table in a JSP page. Each product will have its own table, listing its name,description and price and an add to cart button

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷情妓 2024-11-03 11:05:50

我打算在我的项目的应用程序范围内填充产品。我该如何实现这一目标?

那么您想在 web 应用程序的生命周期内填充它一次吗?使用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 类有一个 List,或者使用 Map Product>> 其中键是类别名称。

至于如何显示它,已经在您的其他问题中得到了解答。

I intend to populate products in the application scope of my project. How do I go about achieving that?

So you want to populate it once during webapp's lifetime? Use a ServletContextListener.

@WebListener
public class StartupListener implements ServletContextListener {

    @Override 
    public void contextInitialized(ServletContextEvent event) {
        List<Product> products = loadItSomehow();
        event.getServletContext().setAttribute("products", products);
    }

    // ...
}

This way the products will be available in every servlet by

List<Product> products = (List<Product>) getServletContext().getAttribute("products");

and in every JSP by

${products}

Secondly, upon populating the products, I want to be able to display each product in a table in a JSP page. Each product will have its own table, listing its name,description and price and an add to cart button

So you want to categorize the products? Have a List<Category> then where Category class has a List<Product>, or use a Map<String, List<Product>> where the key is the category name.

As to how to display it, that's already been answered in your other question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文