Is this the right way to integrate ViewScoped and RequestScoped bean?

I have a page that supports ViewScoped Managed Bean. This page has a dialog box (I use simple fonts) that is supported by RequestScoped ManagedBean. I decided to make the managed bean dialog requested so that it would be cleared at any time when the dialog was launched (the case is mainly used when the user opens the dialog box, fills in some data, and then the data is added to the page supported by ViewScoped Managed Bean).

The way to integrate the two beans is through the ManagedProperty in the RequestScoped Bean dialog box. i.e., the ViewScoped bean is entered into the RequestScoped Bean. When the dialog is saved, the actionListener method in the Dialog RequestScoepd bean updates the property on the ViewScoped bean, which contains a link to the RequestScoped ManagedBean with the current Bean instance. Then, the actionListener in the managed ViewScoped bean is called using the Bean request scope. Thus, an actionListneer in a managed ViewScoped bean can work with the recently introduced RequestScoped ManagedBean.

Is this a good way to do what I'm trying to do, or is there a better way?

Code example:

@ManagedBean @ViewScoped public class PageBackingBean { List<DialogValue> dialogValues; DialogValue dialogValue; public void setDialogValue(DialogValue dialogValue) { this.dialogValue = dialogValue); } public DialogValue getDialogValue() { return dialogValue; } public void handleDialogSave(ActionEvent event) { dialogValues.add(getDialogValue()); } } @ManagedBean @RequestScoped public class DialogValue { @ManagedProperty(#{pageBackingBean}) PageBackingBean pageBackingBean; String val1; String val2; // getters/setters ommitted for brevity... public void dialogSave(ActionEvent event) { pageBackingBean.setDialogValue(this); pageBackingBean.handleDialogSave(event); } } 
+6
source share
1 answer

Collaboration makes sense. Only the DialogValue property and handleDialogSave() are redundant in the PageBackingBean and can be misleading for the future maintainer. Instead, you could only do this with DialogValue bean support.

 public void dialogSave(ActionEvent event) { pageBackingBean.getDialogValues().add(dialogValue); } 

And perhaps rename DialogValue to DialogBacking or something else, at least its name should not be assumed to be just a model.

+2
source

Source: https://habr.com/ru/post/911413/


All Articles