0

I have a command button on my page.

<h:commandButton id="makeSearch" value="#{msg.makeWycena}">
     <f:ajax event="click" render="@all" listener="#{searchBean.makeSearch()}"/>
</h:commandButton>

makeSearch() method first checks value from database, and if this value is null it performs some logic. But when the value isn't null, then I would like to display an error message.

I thought about making <h:outputtext rendered = {not null XXX}/> and make special variable XXX for that reason, but I'm pretty much sure it can be done with plain <h:message /> form, but can't find how.

My code looks like this :

else {
            FacesContext fc = FacesContext.getCurrentInstance();
            fc.addMessage(null , new FacesMessage("XXXXXXXX"));
            return null;
        }

I want the message XXXXXXXXX to be displayed in:

 <h:message for="makeSearch" globalOnly="true" style="color:red;margin:8px;"/>

next to command button I posted before. Right now it's rendered at the bottom of a page


After few changes my code looks like this right now :

<h:form id="detailsForm" prependId="false">
  <h:commandButton id="makeSearch" value="#{msg.makeWycena}">
  <f:ajax event="click" render="@all" listener="#{searchBean.makeSearch()}"/>
</h:commandButton>
<h:message for="makeSearch" id ="makeSearchError" style="color:red;margin:6px;left-margin:10px;"/>

and bean code :

else {FacesContext.getCurrentInstance().addMessage("detailsForm:makeSearchError" ,
   new FacesMessage("XXXXXXXXXXXX")); }

but still. message don't render in h:message. I tried also detailForm:makeSeach and it didn't help either

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Lukas Novicky
  • 921
  • 1
  • 19
  • 44
  • 1
    Use `` for generated messages from server instead of the current ``. – Luiggi Mendoza Oct 08 '13 at 14:03
  • As in my particular page, I have only one message to display this approach is good, I will use this tip :) but still, would like to know how to do it in case I have few error messages to display in seperate message boxes in the future. – Lukas Novicky Oct 08 '13 at 15:39
  • 1
    From [`FacesContext#addMessage`](http://docs.oracle.com/cd/E17802_01/j2ee/javaee/javaserverfaces/2.0/docs/api/javax/faces/context/FacesContext.html#addMessage(java.lang.String, javax.faces.application.FacesMessage)) javadoc, the first parameter must be the client id. Since you're setting `null`, it means it doesn'tfind any component so it will go straight to ``. Refer to this Q/A for more info: http://stackoverflow.com/q/315804/1065197 – Luiggi Mendoza Oct 08 '13 at 15:51
  • FacesContext.getCurrentInstance().addMessage("detailsForm:makeSearchError" , new FacesMessage("XXXXXXXXXXXXX")); I tried that - message still was rendered outside the message box, so I changed message to messages – Lukas Novicky Oct 08 '13 at 16:33

1 Answers1

0

The facesContext.addMessage(null, message) ends up in a <h:messages globalOnly="true">, not in a <h:message for="...">. Note that globalOnly="true" is not supported on <h:message>.

So, replace

<h:message for="makeSearch" globalOnly="true" style="color:red;margin:8px;"/>

by

<h:messages globalOnly="true" style="color:red;margin:8px;"/>

As to your attempt with a fixed message client ID as per the comments, that's also invalid. You should specify the client ID of the component to which the message is associated by its for attribute, not the message component itself. So, you should not have used

facesContext.addMessage("detailsForm:makeSearchError", message);

but instead

facesContext.addMessage("detailsForm:makeSearch", message);

After all, that's unnecessary if you just fix the <h:message> to be a true <h:messages>.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • As I wrote before - I tried both: detailsForm:makeSearch and makeSearchError - none of them worked, so I changed `` to ``and it works. But I only need one error on this page, what if I need more than one? – Lukas Novicky Oct 08 '13 at 17:06
  • Just add one more message by `addMessage()` the same way. The `` supports displaying multiple messages, on the contrary to `` without the plural `s`. – BalusC Oct 08 '13 at 17:10