Can we add a FacesMessage in the actionListener of an ActionSource?

Hi,

I want to show an error message when the user clicks a button. In the actionListener to the button, I wanted to add a message in case a condition is not satisfied. However everytime, I do not see the error message displayed.

The code in my jsp looks as follows:

<h:commandButton id="_commit"

actionListener="#{myBean.doCommit}"

value="Commit"/>

My bean class has the following code:

public class MyBean{

private boolean valid = false; // Hard coded the value for now.

public void setValid(boolean newValue)

{

this.valid = newValue;

}

public boolean getValid()

{

return valid;

}

public void doCommit(ActionEvent event)

{

if (! valid)

{

FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,

"Invalid Selection",

"Invalid Selection");

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

} else {

// Commit the data here

}

}

}

I was not able to find a solution for this problem so far. Any suggestions?

[1130 byte] By [vijay_ea] at [2007-11-26 15:53:04]
# 1
Do you have an h:messages tag in your JSP? Without it, you won't see any of the FacesMessages that you've posted.Also, do you intend for the message to be displayed in the JSP as a result of some incorrect input from the user?
sjscaberta at 2007-7-8 22:13:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...