The h:message tag displays message corresponding to UI element.
The following JSF tag
<h:inputText id="username" size="20" label="UserName" required="true"> <f:validateLength for="username" minimum="5" maximum="20" /> </h:inputText> <h:message for="username" style="color:red" />
If the input is more than 20 characters.
<span style="color:red">UserName: Validation Error: Length is greater than allowable maximum of '20'</span>
If the input is less than 5 characters.
<span style="color:red">UserName: Validation Error: Length is less than allowable minimum of '5'</span>
If the input field is not entered.
<span style="color:red">UserName: Validation Error: Value is required</span>
Attribute | Description |
---|---|
id | id for the tag |
binding | Reference to the component used in a backing bean |
rendered | A boolean value; false would suppress rendering |
styleClass | Cascading stylesheet (CSS) class name |
for | The component ID whose message is displayed |
errorClass | CSS class applied to error messages |
errorStyle | CSS style applied to error messages |
fatalClass | CSS class applied to fatal messages |
fatalStyle | CSS style applied to fatal messages |
globalOnly | Instruction to display only global messages. Default: false |
infoClass | CSS class applied to information messages |
infoStyle | CSS style applied to information messages |
layout | Specification for message layout: table or list |
showDetail | A boolean that determines whether message details are shown. Defaults are false for h:messages, true for h:message |
showSummary | A boolean that determines whether message summaries are shown. Defaults are true for h:messages, false for h:message |
tooltip | A boolean to set whether message details are rendered in a tooltip; the tooltip is only rendered if showDetail and showSummary are true |
warnClass | CSS class for warning messages |
warnStyle | CSS style for warning messages |
style | Inline style information |
title | A title used for accessibility. Browsers typically create tooltips for the title's value |
The following code is from demo.xhtml.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets"> <h:body> <h:form> <h:messages style="color:red;margin:8px;" /> <br /> <h:panelGrid columns="3"> Enter your username : <h:inputText id="username" value="#{user.username}" size="20" required="true" label="UserName" > <f:validateLength minimum="5" maximum="10" /> </h:inputText> <h:message for="username" style="color:red" /> Enter your age : <h:inputText id="age" value="#{user.age}" size="20" required="true" label="Age" > <f:validateLongRange for="age" minimum="1" maximum="200" /> </h:inputText> <h:message for="age" style="color:red" /> </h:panelGrid> <h:commandButton value="Submit" action="result" /> </h:form> </h:body> </html>
The following code is from UserBean.java.
package com.java2s.common; import java.io.Serializable; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean(name="user") @SessionScoped public class UserBean implements Serializable{ private static final long serialVersionUID = 1L; public String username; public int age; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
The following code is from result.xhtml.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"> <h:body> Username : #{user.username} <br /> Age : #{user.age} </h:body> </html>
Copy the generated WAR file from the target folder to Tomcat deployment folder and run Tomcat-Install-folder/bin/startup.bat.
After Tomcat finish starting, type the following URL in the browser address bar.
http://localhost:8080/simple-webapp/demo.xhtml