JSF 不保留原始列表并混合修改后的列表和原始列表或如何在 JSF 2.0 ModelBean 中进行 ArrayList 的深层复制

发布于 2024-12-11 12:51:05 字数 1605 浏览 1 评论 0原文

我在我的应用程序中使用 JSF2.0。 我的项目中的业务需求是当页面加载时显示列表。该列表来自 DAO 层或 Web 服务层。让我们称之为原始列表。 现在我们有 2 个列表,彼此复制,其中只是原始列表,另一个绑定到 JSF

现在列表是可编辑列表,所以我使用 a 来显示列表,并且 中的每个单元格都是 a 。 现在,单击“保存”按钮后,用户修改的列表(因为该列表是可编辑的)将与原始列表进行比较。 仅当两个列表不同时,修改后的列表才会发送回 Web 服务。 我面临的问题是,在 save() 方法本身的第一行中,原始列表和修改后的列表完全相同,并且仅指向修改后的列表。 所以我无法比较这两个列表。

我的实现如下 -

PhaseListener 在页面加载时调用以下方法

 public String populateDataTable() {

        this.orderList = new ArrayList<ItemOrder>();
        this.originalOrderList = new ArrayList<ItemOrder>();
        MockWebService mockService = new MockWebService();
        this.setOrderList(mockService.mockWSMethod());
        this.originalOrderList = this.getOrderList();//i know this is where i am doing something wrong. i need to deep copy a list. but for this i cannot make an additional web service call and call that web service again and store the list in originalOrderList
        return "view";
    }

单击“保存”按钮

    public String saveAction() {
        boolean isSame = true; // true if same and false if different
        for (int i = 0; i < this.originalOrderList.size(); i++) {
            if (!this.originalOrderList.get(i).equals(this.orderList.get(i))) {
                isSame = false;
            }
        }
//however both originalOrderList and orderList have the same modified List.
        if (isSame == true) {
            System.out.println("Same lists");
        } else {
            System.out.println("Different lists");
        }
        return "default";
    }

请帮助我。

提前致谢, 基兰

I am using JSF2.0 in my application.
The business requirement in my project is such that when page loads a list is displayed. The list is coming from DAO layer or web service layer. Lets call this original List.
Now we have 2 lists, copies of each other in which is just the original list and the other is bounded to the JSF

Now the list is editable list so I am using a to display the list and each cell in the is a .
Now on click of SAVE button the modified list by the user (since the list is editable) is compared with the original list.
Only if the 2 lists are different then the modified list is sent back to webservice.
The problem and issue that I am facing is that in the first line of save() method itself both the original and modified list is exactly same and point only to the modified list.
So I am unable to compare the 2 lists.

My implementation is as below-

PhaseListener is calling below method on page load

 public String populateDataTable() {

        this.orderList = new ArrayList<ItemOrder>();
        this.originalOrderList = new ArrayList<ItemOrder>();
        MockWebService mockService = new MockWebService();
        this.setOrderList(mockService.mockWSMethod());
        this.originalOrderList = this.getOrderList();//i know this is where i am doing something wrong. i need to deep copy a list. but for this i cannot make an additional web service call and call that web service again and store the list in originalOrderList
        return "view";
    }

On click of save button

    public String saveAction() {
        boolean isSame = true; // true if same and false if different
        for (int i = 0; i < this.originalOrderList.size(); i++) {
            if (!this.originalOrderList.get(i).equals(this.orderList.get(i))) {
                isSame = false;
            }
        }
//however both originalOrderList and orderList have the same modified List.
        if (isSame == true) {
            System.out.println("Same lists");
        } else {
            System.out.println("Different lists");
        }
        return "default";
    }

Please do help me.

Thanks in advance,
Kiran

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

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

发布评论

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

评论(1

溇涏 2024-12-18 12:51:05

正如您提到的,您面临的问题是 originalOrderListorderList 的浅拷贝(假设 getOrderList() 返回一个 orderList 引用)。在您的情况下,orderList 和originalOrderList 都是指向同一个ArrayList 对象的引用,如果您的orderList 的值发生更改,则originalOrderList 会反映该更改,因为它共享相同的参考。

解决方案是对您的 originalOrderList 进行深度复制:

this.originalOrderList = this.getOrderList().clone(); 

如果您的 ItemOrder 是一个不可变对象,否则:

public static List<ItemOrder> cloneList(List<ItemOrder> list) {
    List<ItemOrder> clone = new ArrayList<ItemOrder>(list.size());
    for(ItemOrderitem: list) clone.add(item.clone());
    return clone;
}

您将必须获取 ItemOrder 对象来实现 Cloneable 接口,并实现clone()方法。

As you mentioned the problem you are facing is originalOrderList is a shallow copy of orderList (assuming getOrderList() is returning an orderList reference). In your case both orderList and originalOrderList are references pointing to the same ArrayList object and if a change is made to the value of your orderList, then the originalOrderList reflects that change because it shares the same reference.

The solution is to do a deep copy of your originalOrderList :

this.originalOrderList = this.getOrderList().clone(); 

if your ItemOrder is an immutable object otherwise :

public static List<ItemOrder> cloneList(List<ItemOrder> list) {
    List<ItemOrder> clone = new ArrayList<ItemOrder>(list.size());
    for(ItemOrderitem: list) clone.add(item.clone());
    return clone;
}

and you will have to get your ItemOrder object to implement the Cloneable interface, and implement the clone() method.

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