5

On clicking the Submit button I have to do application/business level validations and associate the error message to a link. As I cannot do that is there any way that I can place the error message on top of the link.

My validation for business logic is in the action method

FacesMessage message = new FacesMessage();
message.setSeverity(FacesMessage.SEVERITY_ERROR);
message.setSummary("ERROR MESSAGE");
message.setDetail("ERROR MESSAGE");
FacesContext.getCurrentInstance().addMessage("linkId", message);

Help is greatly appreciated

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Greeshma
  • 83
  • 1
  • 2
  • 7

2 Answers2

13

The first argument of FacesContext#addMessage() must be the client ID, not the component ID. The client ID is whatever you see as ID of the HTML element in the generated HTML output (as you can see by rightclick page and View Source in browser). For JSF input and command components in a form this is usually prefixed with the ID of the form.

So, for the following link,

<h:form id="formId">
    ...
    <h:commandLink id="linkId" ... />
    <h:message for="linkId" />
</h:form>

you should be adding the message as follows:

FacesContext.getCurrentInstance().addMessage("formId:linkId", message);

However, the more canonical approach for displaying global messages as you would do from within an action method is just using <h:messages globalOnly="true" /> which you can fill with messages with a null client ID.

So,

<h:form id="formId">
    ...
    <h:commandLink id="linkId" ... />
    <h:messages globalOnly="true" />
</h:form>

with

FacesContext.getCurrentInstance().addMessage(null, message);

See also:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you Baluc .. worked thanks again for your help – Greeshma Jan 27 '12 at 11:44
  • Hi @BalusC `` and in my Controller i have `FacesMessage message = new FacesMessage(); message.setSeverity(FacesMessage.SEVERITY_INFO); message.setSummary("info"); message.setDetail("info"); FacesContext.getCurrentInstance().addMessage(null,message);` but in my logfile i can see: facesmessage was added but maybe never shown... what is wrong? i also tried `FacesContext.getCurrentInstance().addMessage("formId:testFormId",message);` – Joergi Mar 21 '12 at 18:27
  • 1
    @Joerg: Your form and button have the same ID. The message with `null` client ID will only show up in `` (or better, ``. – BalusC Mar 21 '12 at 18:30
  • @BalusC is there any possibility to check in JSF if the FaceMessage are empty? i want to have an error div, which is only shown when the FacesMessage is not empty – Joergi Mar 21 '12 at 23:41
  • @BalusC the `globalOnly="true"` saved me.Thx – Pierre Dec 05 '14 at 14:48
0

You can associate an error message to a link. For this write an action method for your button like below:

<h:commandButton label="Submit" action="#{actionBean.actionMethod}"/>

And the action method as follows:

public String actionMethod(){
  if(error){
     return "error";
  else
     return "index";
}

The method returns the outcome. For example the page is navigated to /error.xhtml if an error occurs.

Ravi Kadaboina
  • 8,494
  • 3
  • 30
  • 42