如果项目列表在刷新之间发生变化,无法使用 EL 2.2 将项目参数传递给 bean?
我使用 EL 2.2 的功能(使用 Tomcat 7)将参数(在本例中为整个项目)从 JSF 页面传递到支持 bean。 事实证明它非常方便,但是当另一个用户在页面刷新之间更改项目列表时,我遇到了一个巨大的问题。 下面是一些示例代码,可帮助说明我的意思:
JSF 页面:
<ui:repeat var="item" value="#{myBackingBean.listOfItems}">
<h:panelGrid columns="3">
<h:outputText value="#{item.name}" />
<p:commandLink value="(Change name to foo)" action="#{myBackingBean.changeNameToFoo(item)}" />
<p:commandLink value="(Delete this item)" action="#{myBackingBean.deleteThisItem(item)}" />
</h:panelGrid>
</ui:repeat>
MyBackingBean.java:
public void changeNameToFoo(Item i) {
i.setName("foo");
}
public void deleteThisItem(Item i) {
i.remove();
}
我的情况是这样的:假设 listOfItems 返回名称为 [1, 2, 3, 4, 5] 的五个项目的列表。两个不同的用户同时加载此页面。用户 A 立即删除了项目 2,现在 参见[1,3,4,5]。用户 B 仍然可以看到所有五个项目,然后尝试将项目 3 的名称更改为 foo。当他的页面刷新时,他现在看到 [1, 3, foo, 5]。因为他甚至不知道一个用户 删除了列表中的第二个项目,与他单击的项目完全不同的项目被更改。
我是否遗漏了一些东西,或者当多个用户进入图片时这真的是一个令人震惊的问题吗?
谢谢!
I'm using EL 2.2's functionality (with Tomcat 7) to pass parameters (in this case, entire items) from a JSF page to a backing bean.
It's proving to be very handy, but I'm running into a gigantic problem when the list of items is changed by another user between page refreshes.
Here's some example code to help show what I mean:
JSF page:
<ui:repeat var="item" value="#{myBackingBean.listOfItems}">
<h:panelGrid columns="3">
<h:outputText value="#{item.name}" />
<p:commandLink value="(Change name to foo)" action="#{myBackingBean.changeNameToFoo(item)}" />
<p:commandLink value="(Delete this item)" action="#{myBackingBean.deleteThisItem(item)}" />
</h:panelGrid>
</ui:repeat>
MyBackingBean.java:
public void changeNameToFoo(Item i) {
i.setName("foo");
}
public void deleteThisItem(Item i) {
i.remove();
}
My situation is this: Say the listOfItems returns a list of five Items with the names [1, 2, 3, 4, 5]. Two different users load up this page at the same time. User A immediately deletes item 2, and now
sees [1, 3, 4, 5]. User B, who still sees all five items, then tries to change item 3's name to foo. When his page refreshes, he now sees [1, 3, foo, 5]. Because a user he wasn't even aware of
removed the second item in the list, a completely different item than the one he clicked was changed.
Am I missing something, or is this really a showstopping problem when multiple users enter the picture?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您允许多个用户共享某些资源,您必须确保相应地处理这种情况。这不仅适用于 JSF,还适用于任何可能存在线程问题的情况。
我不知道您的应用程序有多大,但如果它足够小而不会产生问题,那么我将同步设置列表并从列表中获取信息的方法。
正如我所说,您可能会受到性能影响,这取决于您是否同意(当然还有您的用户)
If you are allowing multiple users share some resources you have to make sure you handle the situations accordingly. And this applies not only to JSF but for any situations that might have threading issues.
I dont know how big your application is but if it is small enough to not create a problem, then i would synchronize the methods that set and get the information from the listOfItems.
As i said, you might take a performance hit and it depends on whether you are OK with that (and your users of course)