JSF 不保留原始列表并混合修改后的列表和原始列表或如何在 JSF 2.0 ModelBean 中进行 ArrayList 的深层复制
我在我的应用程序中使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您提到的,您面临的问题是
originalOrderList
是orderList
的浅拷贝(假设 getOrderList() 返回一个orderList
引用)。在您的情况下,orderList 和originalOrderList 都是指向同一个ArrayList 对象的引用,如果您的orderList
的值发生更改,则originalOrderList
会反映该更改,因为它共享相同的参考。解决方案是对您的
originalOrderList
进行深度复制:如果您的
ItemOrder
是一个不可变对象,否则:您将必须获取 ItemOrder 对象来实现 Cloneable 接口,并实现clone()方法。
As you mentioned the problem you are facing is
originalOrderList
is a shallow copy oforderList
(assuming getOrderList() is returning anorderList
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 yourorderList
, then theoriginalOrderList
reflects that change because it shares the same reference.The solution is to do a deep copy of your
originalOrderList
:if your
ItemOrder
is an immutable object otherwise :and you will have to get your ItemOrder object to implement the Cloneable interface, and implement the clone() method.