0

I have a template with button which opens dialog:

<p:commandButton 
      id="roles-button" 
      icon="fa fa-key">
      <f:setPropertyActionListener value="#{user}" target="#{userAdministrationView.selectedUser}" />
      <f:actionListener binding="#{userAdministrationView.openUserRolesDialogWithParameters()}"/>
          <p:ajax 
               event="dialogReturn" 
               listener="#{dialogHandler.showMessage}"
               update=":user-administration-form:user-administration-table" 
               global="false"
          />
</p:commandButton>

Backing bean for template (userAdministrationView) is @ViewScoped.

I want to pass parameter selectedUser to the dialog. Is it possible with using Faces.setContext/Request/FlashAttribute, like adviced here? I tried to implement it like:

public void openUserRolesDialogWithParameters() {
    Faces.setContextAttribute("user", selectedUser);
    dialogHandler.openDialog("user-roles-dialog");
}

and in dialogs backing bean (which is @ViewScoped too):

@PostConstruct
public void init() {
    this.user = Faces.getContextAttribute("user");
    ...
}

but I get null in user. The same result is with setRequestAttribute and setFlashAttribute.

Here it is suggested to create @SessionScoped bean with properties, but this decision looks not very relevant for me. Is it the only way?

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
T. Kryazh
  • 25
  • 1
  • 9

1 Answers1

0

Thanks to BalusC, i started to search in right direction, and I found this explanation. So, it works like this:

template backing bean :

public void openUserRolesDialogWithParameters() {
    Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
    sessionMap.put("user", selectedUser);
    dialogHandler.openDialog("user-roles-dialog", "contentWidth", "500");
}

dialog backing bean:

@PostConstruct
public void init() {
    Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
    this.user = (UserModel) sessionMap.get("user");
    sessionMap.remove("user");
    ...
}
T. Kryazh
  • 25
  • 1
  • 9