What does <h: messages> do in JSF?
I am learning JSF and came across this line:
<h:messages layout="table"></h:messages> in an example application?
Not sure what this line is doing? I have no error deleting a line from the code, and can I run it and get the same result?
The h:messages tag displays all messages for the current JSF view that are not covered by the h:message tag (mark missing 's at the end). Messages can be generated explicitly using your beans support ( FacesContext.addMessage ) or implicitly using JSF.
eg. if you marked the input value as necessary, and the user submits the form without filling in the required value, an error message will be added to the view. If the h:message tag is attached to the corresponding component, the message will be displayed there, otherwise it will be displayed by the global h:messages tag in your view (if any).
The layout attribute specifies what the HTML code that should be generated should look like. The table layout (used in your example) uses an HTML table to display messages, and the list layout uses a bullet list ( ul tag).
If you do not h:messages tag in your view, as well as the h:message tags, the user will not be informed of errors. Therefore, it is best to include an h:message tag for each input component of your view and an h:messages tag for your entire view to ensure that all messages are visible to the user.
You will find a compact link to JSF tags in the JSF Toolbox .
The <h:message> and <h:messages> components are designed to display messages to users (usually this is an error message).
For example, when you have confirmation in a field that failed (for example, the user did not fill out the required field or did not enter a string in the field for a number only), then FacesMessage added to the FacesContext object. Then <h:message> and <h:messages> are used to display the message on the page.
The <h:messages> component displays all messages contained in the FacesContext , and <h:message> displays for a specific clientId (a specific field). The latter is useful if you want to place a message next to a field, for example.
Please note that you can add any message that will be displayed to the user:
FacesContext.getInstance().addMessage(null, new FacesMessage("The message to display")); In this example, the first parameter is the field identifier field that is associated with this message (useful when the message is a validation message for a specific field). null means that the message is general information (i.e. is not associated with any particular field).
Here you can see an example of this here . Note that this example uses rich:messages , which is an extension (provided by RichFaces) of the "main" <h:message/> , but the principle is the same.