JSFJSF用戶界面組件模型
JSF UI組件示例
JSF <h:inputText>標籤
JSF <h:outputText>標籤
JSF <h:form>標籤
JSF <h:commandButton>標籤
JSF <h:inputtextarea>標籤
JSF <h:commandLink>標籤
JSF <h:inputSecret>標籤
JSF <h:inputHidden>標籤
JSF <h:inputFile>標籤
JSF <h:graphicImage>標籤
JSF <h:message>標籤
JSF <f:ajax>標籤
JSF單選按鈕
JSF表單組合框
JSF列表框
JSF多選列表框
JSF輸出格式化
JSF輸出樣式
JSF <h:attribute>標籤
JSF <h:setPropertyActionListener>標籤
JSF驗證正則表達式
<f:validateRegex>
標籤用於將字符串值驗證爲所需格式。
以下代碼顯示如何使用<f:validateRegex>
標記。
<f:validateRegex pattern="((?=.*[a-z]).{6,})" />
標籤屬性
屬性
說明
pattern
格式化模式
JSF驗證整數範圍實例
打開 NetBeans IDE 創建一個Web工程:ValidateRegularExpression,其目錄結構如下所示 -
創建以下文件代碼,文件: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">
<h:body>
<h:form>
<h:panelGrid columns="3">
Enter your password :
<h:inputSecret id="password" value="#{user.password}"
size="20" required="true"
label="Password">
<f:validateRegex pattern="\d\d\d\d" />
</h:inputSecret>
<h:message for="password" style="color:red" />
</h:panelGrid>
<h:commandButton value="Submit" action="result" />
</h:form>
</h:body>
</html>
文件: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"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core"
>
<h:body>
<h:panelGrid columns="2">
Password : <h:outputText value="#{user.password}" />
</h:panelGrid>
</h:body>
</html>
文件:User.java 的代碼內容如下所示 -
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.yiibai;
/**
*
* @author Maxsu
*/
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name = "user")
@SessionScoped
public class User implements Serializable {
String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
右鍵運行工程:ValidateRegularExpression,如果沒有任何錯誤,打開瀏覽器訪問:
http://localhost:8084/ValidateRegularExpression/
應該會看到以下結果 -
簡單寫入一些信息(密碼要求必須是4
位整數),然後提交 -