Basic error handling
Seam, JSF and Java all team up together to help developers in displaying error messages to the user.
JSF provides the basis, with the FacesMessage mechanism. Anywhere in your code, you can create FacesMessages, and add them to a FacesMessages object. Then, when the next page displays, the error messages are displayed to the user.
You can use special tags for that in your .xhtml page:
1 2 | <ice:messages id="messages" globalOnly="true" styleClass="message" errorClass="errormsg" infoClass="infomsg" warnClass="warnmsg"/> |
Notice icefaces allows us to set various css style depending on the type of error.
The result is here:
Now, when a validation is bad, we want to highlight the input field, then display the error message next to it. Fortunately, JSF allows a developer to assign an error message to a field, and by default the validators and converters do just that.
And Seam, allows us to define specific html code when a field is in error. This is done using the s:decorate tag, and the best option is to refer to a template file that will be reused for all fields.
Now the input field is declared like that:
1 2 3 4 5 | <s:decorate template="/layout/decorate.xhtml"> <ice:inputText id="nbElements" value="#{searchPage.nbElements}" /> <ice:message for="nbElements" styleClass="message" errorClass="errormsg" infoClass="infomsg" warnClass="warnmsg"/> </s:decorate> |
and for the field that must not be null or empty, we just add an required= »true » attribute:
1 2 3 4 5 | <s:decorate template="/layout/decorate.xhtml"> <ice:inputText id="notNull" value="#{searchPage.notNull}" required="true" /> <ice:message for="notNull" styleClass="message" errorClass="errormsg" infoClass="infomsg" warnClass="warnmsg"/> </s:decorate> |
and the content of the error template file is this:
1 2 3 4 5 6 7 | <div class="errorDiv"> <span class="#{invalid?'errors':''}> <s:validateAll> <ui:insert/> </s:validateAll> </span> </div> |
The errors css style just sets the component border to Red.
Now, if we try some basic errrors, like by inputing letters in a field mapped to an integer value, here is the result:
Notice the error message is more precise than the previous screenshot. Any error message can be changed in JSF by a simple properties file.
That’s all for basic error handling. Next time, we’ll try to see the behavior of the current page for all errors defined in the first part.













Nice isn’t it ? 









Remember this game now ?



