从另一个访问一个托管bean [英] Accessing one managed bean from another

查看:117
本文介绍了从另一个访问一个托管bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的授权页面,旨在让用户键入登录名和密码.根据用户输入的结果,我定义要显示的下一页.我在db中有一个表,其中包含ID,用户的登录名和密码以及用户的角色,因此将根据将从记录的角色字段中检索到的信息(与输入的登录名和密码相对应)来定义下一页.而且我在db中有一个表,其中包含一种类型的用户的所有用户信息(大约10个字段),而另一个表则包含有关另一种类型的用户的信息(4个字段).所以我的数据库有这样的结构:

I have a simple authorization page which intends the user to type login and password. Based on the results of user input, I define the next page to display. I have a table in db that contains id, user's login and password, and user's role, so the next page will be defined based on information that will be retrieved from the role field of the record which corresponds to inputed login and pass. But also I have a table in db which contains all of the user's info(about 10 fields) of one type of users and another table which contains info about another type of users(4 fields). So my db has such a structure:

Table 1:
  id
  login
  pass
  role

Table 2:
  id(reference to id of table 1)
  other fields....

Table 3:
  id(reference to id of table 1)
  other info....

我认为我的应用程序必须具有托管的bean(requestScope)以进行授权,该授权将使用登录名和密码.该bean将具有一个用于提交授权表单的动作控制器.在该动作控制器中,我必须确定用户的角色,然后根据该信息为特定类型的用户创建一个会话范围的托管Bean(应用程序将为不同类型的用户使用单独的托管Bean).我也有在该动作控制器中传递特定的信息以实例化用户(用户ID)的会话作用域bean.但是,我是否可以在请求范围托管Bean的操作方法中简单地使会话范围托管Bean(借助new运算符)无效?另外,我如何在另一个会话范围的bean中使用一个会话范围的bean的方法(假设我创建了2个会话范围的托管bean,它们负责不同的服务,我又该如何调用另一个方法的范围). 也许所描述的方法不适用于此类问题?如果可以的话,您可以提出更好的设计方法建议吗?

I think that my app has to have a managed bean(requestScope) for authorization that will take login and password. That bean will have an action controller for submitting authorization form. In that action controller I have to determine the role of user and based on that information create a session scoped managed bean for concrete type of user(app will have separate managed bean for different types of users). Also I have in that action controller to pass specific info for instatiation the session scoped bean for user(id of user). But can I simply instatiate a session scope managed bean(with help of new operator) in action method of request scope managed bean? Also how can I use methods of one session scoped bean in another session scoped bean(Suppose I create 2 session scoped managed beans which are responsible for different services, how can i call method of one in another). Maybe the described method isn't good for such a problem? If so can you give an advise for better design approach?

推荐答案

从另一个访问一个托管bean

要直接回答此问题,通常使用 @ManagedProperty 将一个托管bean注入另一个托管bean中.是的,它将自动创建bean,但这应该特别便宜.

To answer this directly, you normally use @ManagedProperty to inject one managed bean in another managed bean. Yes, it will auto-create the beans, but that should be particularly cheap.

但是我可以在请求范围托管Bean的操作方法中简单地(在新操作符的帮助下)将会话范围托管Bean实例化吗?

是的,那是可能的.只需将其存储在 ,以受管bean名称为键.

Yes, that's possible. Just store it in ExternalContext#getSessionMap() with the managed bean name as key.

ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.getSessionMap().put("bean", new Bean());

#{bean}可用,依此类推.那是否是一个好的方法,则是第二个问题.

It'll be available by #{bean} and so on. Whether that's a good approach is a second.

关于您的具体功能要求.我认为最好是将单个会话范围的托管bean带有所需的数据作为属性,这要好得多.然后,您可以例如仅检查User属性是否不是null以确定用户是否已登录.您可以在以下答案中找到一个Bean示例:

As to your concrete functional requirement. I think it's much better to have just a single session scoped managed bean with the desired data as properties. You could then for example just check if the User property is not null to determine if the user is logged in. You can find a bean example in this answer: Programmatically control login with Servlet 3.0.

这篇关于从另一个访问一个托管bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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