Reputation: 83
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
Upvotes: 5
Views: 12804
Reputation: 1108632
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);
Upvotes: 13
Reputation: 8574
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.
Upvotes: 0