鍍金池/ 教程/ HTML/ JSF <h:messages>標(biāo)簽
JSF數(shù)據(jù)表(h:dataTable)添加刪除
JSF <h:commandLink>標(biāo)簽
JSF應(yīng)用程序入門(mén)示例
JSF數(shù)據(jù)表(ui:repeat)創(chuàng)建表
JSF列表框
JSF數(shù)據(jù)表(h:dataTable)DataModel排序數(shù)據(jù)
JSF復(fù)合組件
JSF <h:inputText>標(biāo)簽
JSF表單組合框
JSF <h:messages>標(biāo)簽
JSF <h:message>標(biāo)簽
JSF轉(zhuǎn)換日期時(shí)間
JSF JDBC連接
JSF <h:inputHidden>標(biāo)簽
JSF多選列表框
JSF <h:inputSecret>標(biāo)簽
JSF自定義轉(zhuǎn)換器
JSF <f:ajax>標(biāo)簽
JSF生命周期
JSF可重定位資源
JSFJSF用戶界面組件模型
JSF <h:attribute>標(biāo)簽
JSF驗(yàn)證器標(biāo)簽
JSF驗(yàn)證字符串長(zhǎng)度
JSF轉(zhuǎn)換器標(biāo)簽
JSF托管bean(Managed Bean)
JSF值變化的事件
JSF UI組件示例
JSF MySQL CURD實(shí)例
JSF數(shù)據(jù)表(h:dataTable)排序數(shù)據(jù)
JSF <h:graphicImage>標(biāo)簽
JSF <f:convertNumber>標(biāo)簽
JSF教程
JSF托管Bean
JSF輸出腳本
JSF <h:outputText>標(biāo)簽
JSF操作事件
JSF驗(yàn)證正則表達(dá)式
JSF數(shù)據(jù)表(h:dataTable)行號(hào)
JSF <h:setPropertyActionListener>標(biāo)簽
JSF注入托管bean實(shí)例
JSF <h:commandButton>標(biāo)簽
JSF Web資源
JSF <h:inputFile>標(biāo)簽
JSF驗(yàn)證浮點(diǎn)數(shù)值范圍
JSF Facelets視圖
JSF是什么?
JSF Facelets模板
JSF的特性(特點(diǎn))
JSF自定義驗(yàn)證器類
JSF單選按鈕
JSF輸出樣式
JSF數(shù)據(jù)表(h:dataTable)更新數(shù)據(jù)
JSF HTML5友好標(biāo)記
JSF表單復(fù)選框(CheckBox)示例
JSF <h:form>標(biāo)簽
JSF Facelets技術(shù)介紹
JSF輸出格式化
JSF <h:inputtextarea>標(biāo)簽
JSF驗(yàn)證整數(shù)范圍
JSF <h:panelGrid>標(biāo)簽

JSF <h:messages>標(biāo)簽

<h:messages>標(biāo)簽顯示與UI元素相對(duì)應(yīng)的一個(gè)地方的所有消息。
以下JSF標(biāo)簽 -

<h:messages style="color:red;margin:8px;" />

如果用戶名輸入超過(guò)20個(gè)字符,輸入的密碼小于5個(gè)字符。渲染結(jié)果如下 -

<ul style="color:red;margin:8px;">
   <li>  UserName: Validation Error: 
   Length is greater than allowable maximum of '20' </li>
   <li>  Password: Validation Error: 
   Length is less than allowable minimum of '5' </li>
</ul>

實(shí)例

以下是文件: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.yiibai;

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;
  }

}

以下是文件: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>

運(yùn)行測(cè)試

打開(kāi) NetBeans創(chuàng)建一個(gè)名稱為:Messages 的Web工程,并使用以上代碼。發(fā)布工程,Tomcat啟動(dòng)完成后,在瀏覽器地址欄中輸入以下URL。

http://localhost:8084/Messages

得到以下結(jié)果 -