Adding error messages in JSF


When developing applications with JSF, there is the need to display error messages back to the user, if a user submitted form contains errors, or if the processing of the data went wrong.

For this request, JSF meets the <h:message/> tag.

Here’s how you can display error messages back to the user:

Practically, the tag acts as a holder, or as a div inside which the error message can be displayed. You can put this tag wherever you think it visually shows better for the user to notice it.
This tag used to display the most recent message for the component, and it needs to be linked with
a specific component (JSF component like an input field that needs to have its value checked), for which it will hold the error messages. This is done by using the for attribute for it.

<h:message for="catName"/>
<h:inputText value="#{}" id="catName" />

Notice how the for attribute’s value matches the component id. Each time an invalid value is entered inside the input, the error message associated for it, will display in place of the tag.

Note! that this binding will only work for JSF elements. If you try to bind the tag to a regular HTML element’s id, it will not display the error message on page, but rather only inside the application’s server log.

How do you decide which values are considered valid and which are not?

You can do two kinds of validation:

  • JSF Standard Validators
  • Custom server side validation

JSF Standard Validators
When using the standard Validator implementations, you don’t need to write any code to perform validation.
There are three standard JSF validators:

  • <f:validateLength/> for validating a String with a minimum and maximum number of characters
  • <f:validateDoubleRange/> for validating a double value within an optional range
  • <f:validateLongRange/&gt; for validating a long value within an optional range

Validate whether the length of the value is in the [1,10] range

<h:message for="name" />

<h:inputText id="name" required="true">
      <f:validateLength maximum="10" minimum="1"/>

Custom server side validation
You can build your own custom validator (not described in this article, but you can find it here)
Or, at any time in your application logic, you can get the faces context and add an error message for a specific component.

FacesContext context = FacesContext.getCurrentInstance();
FacesMessage message = new FacesMessage("Error message here");
context.addMessage(componentId, message);

And this will display the error message inside the <h:message/> tag associated with the JSF component (can be a <h:form>, <h:dataTable> or any other JSF component)

These error messages can also have a CSS style class associated with them, for each level of error severity. This can be set by using some of the provided attributes for the element:

  • errorClass CSS class applied to error messages
  • fatalClass CSS class applied to fatal messages
  • infoClass CSS class applied to information messages
  • warnClass CSS class for warning messages

Finally, there’s another very important peculiarity of what does Cialis that brings it so high above its alternatives. It is the only med that is available in two versions – one intended for use on as-needed basis and one intended for daily use. As you might know, Viagra and Levitra only come in the latter of these two forms and should be consumed shortly before expected sexual activity to ensure best effect. Daily Cialis, in its turn, contains low doses of Tadalafil, which allows to build its concentration up in your system gradually over time and maintain it on acceptable levels, which, consequently, makes it possible for you to enjoy sex at any moment without having to time it.

2 thoughts on “Adding error messages in JSF”

Comments are closed.

By continuing to use the site, you agree to the use of cookies. More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.