簡體   English   中英

JSF:將對象從一個支持豆傳遞到另一個支持豆

[英]JSF : Passing an Object from one backing-bean to another backing-bean

我正在嘗試為開發小型應用程序時遇到的小問題找到解決方案。 我試圖傳遞在一個后備bean中創建的對象,然后再使用在另一個后備bean中創建的相同對象。 但是,我不希望將這些backing-beans設置為@SessionScoped ,並且最好不要使用@ManagedBean因為我在JavaEE應用程序中使用了CDI

無論如何,我可以使用CDI批注並將一個backing Bean注入到另一個back-bean中,然后能夠訪問先前創建的對象嗎?

作為示例,請參考以下bean:

@Named
@ViewScoped
public RegisterController implements Serializable {

    User user = new User();

    // Getter and Setter methods which are populated by the JSF page
}

獲取在上述bean中創建的對象User ,並在以下控制器中使用它:

@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...
}

我可以使用CDI提供的@Inject注釋嗎?

UPDATE

好的,所以當我使用SessionScoped批注對RegisterController進行批注時,我已經完成了上述工作,但是我不希望將此Bean批注為SessionScoped因為我很可能會遇到進一步的影響,例如字段的預填充等。 ..有什么想法可以通過其他方式實現嗎?

好吧, @ViewScoped太短了,而@SessionScoped太長了。 那為什么不使用@ConversationScoped

這里

您可以在RegistrationController中使用:

FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("user", user);

並在PaymentController中:

FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user");

這非常有用,您可以將所需的對象保存在地圖中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM