我正在尝试为开发小型应用程序时遇到的小问题找到解决方案。我试图传递在一个后备bean中创建的对象,然后再使用在另一个后备bean中创建的相同对象。但是,我不希望使用这些backing-beans @SessionScoped
,并且最好不要使用@ManagedBean
我在CDI
JavaEE应用程序中使用的。
无论如何,我可以使用CDI
批注将一个后备bean注入到另一个中,然后能够访问先前创建的对象吗?
作为示例,请参考以下bean:
@Named
@ViewScoped
public RegisterController implements Serializable {
User user = new User();
// Getter and Setter methods which are populated by the JSF page
}
获取User
在上述bean中创建的对象,并在以下控制器中使用它:
@Named
@ViewScoped
public PaymentController implements Serializable {
@Inject RegisterController registerController; // Injecting bean
registerController.getUser().getName(); //this is null when I try access the properties of the object 'User'. I am assuming this is because @ViewScoped annotated on the 'RegisterController' class?
// I would like to use the User object created in 'RegisterController' here an access properties etc...
}
我可以使用@Inject
CDI提供的注释吗?
更新
好了,我已经得到了上面的工作时,我注释RegisterController
与SessionScoped
注释,但我不希望这个bean的注解SessionScoped
,因为我可能会碰到走下赛场进一步影响,如字段的预填充,等等...任何想法如何以其他方式实施?
好吧,@ViewScoped
太短了,目的@SessionScoped
太长了。那为什么不使用@ConversationScoped
呢?
看这里。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句