如何更新数组列表

bqf10yzr  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(437)

我正在尝试向html表添加更新函数。现在我有一个insert和delete,但我正在试图找出如何添加一个update函数。我已经在这上面呆了一段时间了。我试着添加.edit,但似乎不起作用。
谢谢你的帮助。谢谢您。
这是我的服务课程:

@Service
    public class ProductService {

        static List<Product> productList = new ArrayList();

        static {
            productList.add(new Product("XG809", "Contemporary Office Desk", "Concentrating on the job is a lot easier when everyone has a place that’s comfortable to work at.", 25.00, 69.00, 18));
            productList.add(new Product("BG565", "Bed Frame With Storage", "With the comfort and quality you get from our sturdy single beds, you’ll wake up refreshed and ready to roll. ", 139.00, 175.00, 5));
            productList.add(new Product("PO262", "TV Stand", "Our TV stands and TV cabinets are there to cut the clutter and get things organised.", 69.99, 89.99, 120));
            productList.add(new Product("MC342", "Kitchen Unit", "They make the most of your wall by giving you extra storage, and the right kitchen shelf can boost the style of your decor too", 23.00, 65.99, 89));
            productList.add(new Product("WS341", "Folding Chair", "You can fold the chair, so it takes less space when you're not using it.", 12.00, 35.99, 30));
            productList.add(new Product("TF875", "Berkant Kitchen", "Express yourself in the place where all of life’s daily activities take place.in our stylish, yet personalised kitchen..", 8900.00, 12200.99, 4));

        }

        public List<Product> getAllProducts() {
            return productList;
        }// end getAllProducts

        public boolean addAProduct(Product p) {
            return productList.add(p);
        }

        public void deleteAProduct(String code) {

            Iterator<Product> iterator = productList.iterator();
            while (iterator.hasNext()) {
                Product product = iterator.next();
                if (product.getCode().equalsIgnoreCase(code)) {
                    iterator.remove();
                }
            }
        }//end deleteAProduct

        public boolean editAProduct (Product p) {
            return productList.edit(p);
        }
    }

这是我的jsp页面

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>All Products</title>
    </head> 
    <body>
        <security:authorize access="hasRole('SUPERADMIN')"><p>Welcome ${uname}, <a href="/logout">Logout</a></p></security:authorize>
        <security:authorize access="hasRole('ADMIN')"><p>Welcome ${uname},<a href="/logout">Logout</a></p></security:authorize>
        <security:authorize access="hasRole('USER')"><p>Welcome ${uname},<a href="/logout">Logout</a></p></security:authorize>
        <table style="width:100%">
            <tr>
                <th align="left">Code</th>
                <th align="left">Name</th>
                <th align="left">Description</th>
                <th align="left">Buy Price</th>
                <th align="left">Sell Price</th>
                <th align="left">Qty In Stock</th>
                <th align="left">Actions</th>
            </tr>
            <c:forEach items="${productList}" var="product"> 
                <tr>
                    <td>${product.code}</td>
                    <td>${product.name}</td>
                    <td>${product.description}</td>
                    <td><fmt:setLocale value="en_EUR"/>
                        <fmt:formatNumber value="${product.buyPrice}" type="currency"/></td>
                    <td><fmt:formatNumber value="${product.sellPrice}" type="currency"/></td>
                    <td>${product.quantityInStock}</td>
                    <td><security:authorize access="hasRole('SUPERADMIN')">
                            <a href="addProduct">Insert</a>
                            <a href="/admin/deleteProduct?code=${product.code}">Delete</a>
                        </security:authorize>
                        <security:authorize access="hasRole('ADMIN')">
                            <a href="/admin/deleteProduct?code=${product.code}">Delete</a>
                        </security:authorize>
                    </td>

                </tr>
            </c:forEach>
        </table>

    </body>
</html>
vs91vp4v

vs91vp4v1#

arraylist方法集(int index,e element)用于更新列表的元素。将列表(索引)中的位置和新值传递给它。

相关问题