它用于显示特定组件的单个消息。您可以通过将该组件的id传递给for属性来显示您的自定义消息。
以下JSF标签 -
<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" />
如果输入超过20个字符时提示 -
<span style="color:red">UserName: Validation Error: 
Length is greater than allowable maximum of '20'</span>
如果输入小于5个字符时提示 -
<span style="color:red">UserName: Validation Error: 
Length is less than allowable minimum of '5'</span>
如果输入字段未输入时提示 -
<span style="color:red">UserName: Validation Error: Value is required</span>
JSF <h:graphicImage>标签的属性
| 标签 | 描述 | 
|---|---|
| for | 它是用于分配组件ID的强制性标签,因为该消息是组成的。 | 
| errorClass | 它用于将CSS样式类应用于严重性类为“ ERROR”的任何消息。 | 
| errorStyle | 它用于将CSS样式应用于严重性级别为“ ERROR”的任何消息。 | 
| fatalClass | 它用于将CSS样式类应用于严重性级别为“ FATAL”的任何消息。 | 
| FatalStyle | 它用于将CSS样式应用于严重性级别为“ FATAL”的任何消息。 | 
| infoClass | 它用于将CSS样式类应用于严重性级别为“ INFO”的任何消息。 | 
| InfoStyle | 它用于将CSS样式应用于严重性级别为“ INFO”的任何消息。 | 
| tooltip | 它用于将消息的详细信息部分显示为工具提示。 | 
| warnClass | 它用于将CSS样式类应用于严重性类为“ WARN”的任何消息。 | 
| warnStyle | 它用于将CSS样式应用于严重性级别为“WARN”的任何消息。 | 
实例
文件: index.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>
文件: UserBean.java 中有如下代码 -
package com.zaixian;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name="user")
@SessionScoped
public class User 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;
  }
}
以下是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>
运行项目
启动Tomcat完成后,在浏览器地址栏中输入以下URL。
http://localhost:8080/hjsf/index.xhtml
运行结果如下 -

未输入值直接提交,结果如下 -

输入值后提交,结果如下 -

						上一篇:
								JSF <h:graphicImage>标签
												下一篇:
								JSF <f:ajax>标签
												
						
						
					
					
					