0

I have a <p:dialog> in my general layout. I have its Header attribute hardcoded at the moment.

What I want is to access it from different beans and change its Header at run-time according to my choice.

I am using it to show a loading message to the user at the moment and want to update the loading text according to the current backend processing, e.g "waiting for server's response" etc.

<p:dialog id="main-status-dialog"
          widgetVar="mainStatusDialog"
          modal="true"
          header="Loading..."
          draggable="false"
          closable="false"
          resizable="false"
          appendToBody="true">

Now I am calling it from different JSF pages on button clicks e.g <h:link outcome="/generalInformation" value="General Information" onclick="mainStatusDialog.show()" /> It works fine but always Show me "Loading..." because I have a hardcoaded Attribute. So how can I make it dynamic? Please note that I don't want to do it only for one page or bean, but from any page it Access it, i can Change ist Header accordingly. Thanks!

Undertaker
  • 111
  • 2
  • 13

1 Answers1

0

You can use a managed property with @ManagedProperty of one of your managed beans (e.g HeaderBean)and change it every time accordingly and set the header value to this value, and it whould look dynamically updated.

@ManagedProperty("#{headerBean}")
private HeaderBean headerBean;

And in your header managed bean make a String property value where you will store the value of the header:

@ManagedBean(name = "headerBean")
@RequestScoped
public class HeaderBean implements Serializable
{
    private String value = null;

    // getter and setter methods

And in your p:dialog:

<p:dialog id="main-status-dialog"
      widgetVar="mainStatusDialog"
      modal="true"
      header="#{headerBean.value}"
      draggable="false"
      closable="false"
      resizable="false"
      appendToBody="true">

Take a look at the following liks to find more about it:

  1. The BalusC Code: Communication in JSF 2.0
  2. Injecting Managed beans in JSF 2.0
  3. @ManagedProperty - Inject one request scoped bean into another request scoped bean

EDIT:

You can use RequestContext to update the dialog from your beans, If you take a look at Better ajax operations and callbacks in JSF with PrimeFaces you will see that:

RequestContext API enables developers to do 2 simple things. 1st you can tell what you want to update in the xhtml based on conditions in actions that you define in your Managed Beans.To update a component from serverside, you can just write:

The code you need to update the p:dialog from your managed beans is:

RequestContext.getCurrentInstance().
          addPartialUpdateTarget("header_id");

You can also use update attribute in your commandLink like this:

<h:link outcome="/generalInformation" value="General Information" oncomplete="mainStatusDialog.show()" update=":main-status-dialog"/>
Community
  • 1
  • 1
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
  • Hi, thanks for the answer. I tried but it didn't update the Header. May be I have to update the Dialog after changing the message? – Undertaker Jun 29 '15 at 09:43
  • of course you have to update the dialog so the header will be changed. – cнŝdk Jun 29 '15 at 09:52
  • then for this I have to do something from bean. can you give me the code how i can Access the Dialog in bean an update it? – Undertaker Jun 29 '15 at 10:15
  • it worked with RequestContext.getCurrentInstance().update( "main-status-dialog" ); but the Problem is when I Change it, the value becomes permanently changed. Also it doesn't Change the value live when a previous Dialog is already displaing. I have tried it with @RequestScoped, view and session but same results. – Undertaker Jun 29 '15 at 11:41
  • As far as I know you can't do it permanently, you can either use an update attribute with another component such as a link, take a look at [this link](http://stackoverflow.com/questions/10659389/primefaces-how-to-update-content-in-a-dialog-and-keep-the-dialog-centered) or like I mentioned with RequestContext . – cнŝdk Jun 29 '15 at 11:57
  • Thanks for the answer, tough it hasn't solved my issue completely but it was helpfull :) – Undertaker Jun 30 '15 at 12:36