我应该使用哪种托管bean作用域? [英] What managed bean scope should I use?

查看:122
本文介绍了我应该使用哪种托管bean作用域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在3个不同的页面上使用用户列表".

I have 3 different page where I use list of Users.

第一页包含dataTable,其中包含来自一个会话范围托管Bean的用户.在该页面上,我可以更改选定的用户详细信息.

First page contains dataTable with users from one session scope managed bean. On that page I can change selected user details.

第二页还包含带有用户的dataTable,但它们来自不同的会话范围Bean.在此页面上,我也从dataTable(用户列表)和数据库中删除用户.

Second page also contains dataTable with users, but from different session scope bean. On this page I delete users from dataTable (list of users) and data base too.

我意识到这不是一个好主意,因为如果我对一个用户进行更改,则该更改将不会显示在删除用户的页面上.

I realized that this is not good idea, because if I made changes on one user, that changes will not be shown on page where I delete users.

仅用户列表的应用程序范围bean是否是解决方案?

Is application scope bean with only list of users a solution?

推荐答案

仅具有用户列表的应用程序范围bean是否是解决方案?

否,它不能解决您面临的问题,毕竟您会遇到并发问题,因为Web应用程序通常是多用户环境,因此您需要将所有内容都做成synchronized以防止讨厌ConcurrentModificationException等.即,应用程序范围的Bean在Webapp的 all 用户之间共享.即使会话范围太宽,将请求/视图范围的数据保存在应用程序范围的Bean中也没有任何意义.

No, it won't solve the problem you're facing and you'll after all run into concurrency issues as a webapplication is usually a multi-user environment and you'd need to make everything synchronized to prevent nasty ConcurrentModificationException and like. An application scoped bean is namely shared between all users of the webapp. It makes no sense to hold request/view scoped data in an application scoped bean, even the session scope is too broad.

因此,您的问题是会话范围太宽.您想在每个新的GET请求中刷新列表.最好的实现方法是将bean放入请求中,或者更好地查看范围.用于编辑用户详细信息的bean可以是另一个bean,但是当您不想使编辑页面成为可标记的GET请求时,也可以轻松地将其仅仅是第一个bean的属性.

Your problem is thus that the session scope is too broad. You'd like to refresh the list on every fresh new GET request. Best to achieve this is to put the bean in the request or, better, view scope. The bean which edits the user detail could be another one, but this can in turn also easily be a just a property of the first bean whenever you don't want to make the edit page a bookmarkable GET request.

  • Preserving bean values between views
  • How to choose the right bean scope?

这篇关于我应该使用哪种托管bean作用域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆