1
votes

I have doubt regarding how to show facescontext messages in a dialog box. This uses primefaces 4.0, JSF.

I want to display a facescontext message in pop up dialog box (which appears on clicking a commandbutton in another dialog box).

Method 1 : Suppose the view file is like

<p:commandButton id=”btn”  oncomplete=”dlg.show()”/>
<p:dialod id=”dlg_id” widgetVar=”dlg”>
    <h:inputText id=”name”/>
    <p:commandButton id=”btn1” actionListener=”someBean.someMethod()” oncomplete=”dlg1.show()”/>
</p:dialog>
<p:dialog id=”dlg1_id” widgetVar=”dlg1”>
    <h:messages id=”error_msgs” value=”#{facesContext.messageList}”
</p:dialog>

BackingBean (someBean)

public void someMethod() {
        RequestContext.getCurrentInstance().addCallBackParam(“facesMessageAvailable”,true);
        FacesContext.getCurrentInstance().addMessage(“error_msgs”,new FacesMessage(…,”Name is Required”,…));
    }

The above method shows the pop up box. But the value shown in the pop up box is like javax.beans.context@1ggh34ea Then I tried using UI component binding.

Method 2 :

View File

<p:commandButton id=”btn”  oncomplete=”dlg.show()”/>
<p:dialog id=”dlg_id” widgetVar=”dlg”>
    <h:inputText id=”name”/>
    <p:commandButton id=”btn1” actionListener=”someBean.someMethod()” oncomplete=”dlg1.show()”/>
</p:dialog>
<p:dialog id=”dlg1_id” widgetVar=”dlg1”>
    <h:outputText id=”msg” binding=”someBean.outText”/>
</p:dialog>

BackingBean (someBean)

private UIComponent outText;
//getter and setter of outText
public void someMethod() {
    RequestContext.getCurrentInstance().addCallBackParam(“facesMessageAvailable”,true);
    FacesContext.getCurrentInstance().addMessage(outText.getClientId() , new FacesMessage(…,”Name is Required”,…));
}

But this shows a blank empty pop up. Then I tried using JOptionPane. But it produces some logical errors.

I would appreciate any help. The syntax may be incorrect as i typed the code from my memory.

1

1 Answers

1
votes

I tried your Method 1 and found there are some mistakes:

Using <h:messages>

1.You should specify update attribute of the <p:commandButton>.
2.No need to specify value=”#{facesContext.messageList}” of <h:messages> (And there's also no such attribute).

So, after doing those, your code should like this:

    <h:form>
        <p:commandButton id="btn"  oncomplete="dlg.show()"/>

        <p:dialog id="dlg_id" widgetVar="dlg">
            <h:inputText id="name"/>
            <p:commandButton id="btn1" actionListener="#{someBean.someMethod()}" oncomplete="dlg1.show()" update="dlg1_id"/>
        </p:dialog>

        <p:dialog id="dlg1_id" widgetVar="dlg1">
            <h:messages id="error_msgs"/>
        </p:dialog>

    </h:form>  

And the result looks like:
enter image description here


Using <h:message>

It's using <h:messages> above, while you may want to use <h:message> as you seems want the message show for error_msgs. To do so, you need to specify the client_id instead of id, so try this:

Page:

    <h:form>
        <p:commandButton id="btn"  oncomplete="dlg.show()"/>

        <p:dialog id="dlg_id" widgetVar="dlg">
            <h:inputText id="name"/>
            <p:commandButton id="btn1" actionListener="#{someBean.someMethod()}" oncomplete="dlg1.show()" update="dlg1_id"/>
        </p:dialog>

        <p:dialog id="dlg1_id" widgetVar="dlg1">
            <h:message for="j_idt5"/>
        </p:dialog>
    </h:form> 

Backing Bean:

public void someMethod(){
    RequestContext.getCurrentInstance().addCallbackParam("facesMessageAvailable",true);
    FacesContext.getCurrentInstance().addMessage("j_idt5",new FacesMessage("Name is Required"));
}

Note that j_idt5 is the client_id of the <h:inputText>.
How can I know the client_id? Just right click on the page in the browser and View page source code, look at the component you are interested in and you'll find the cliend_id.