<f:validateLongRange>
標簽用于驗證特定范圍內(nèi)的長值。
以下代碼顯示如何使用<f:validateLongRange>
標記 -
<f:validateLongRange minimum="5" maximum="200" />
屬性 | 說明 |
---|---|
minimum | 在可選范圍內(nèi)最小長度值 |
maximum | 在可選范圍內(nèi)最大長度值 |
打開 NetBeans IDE 創(chuàng)建一個Web工程:ValidateIntRange,其目錄結(jié)構(gòu)如下所示 -
創(chuàng)建以下文件代碼,文件:index.xhtml 的代碼內(nèi)容如下所示 -
<?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:form>
<h:panelGrid columns="3">
Enter your age :
<h:inputText id="age" value="#{user.age}"
size="10" required="true"
label="Age" >
<f:validateLongRange maximum="10" minimum="1" />
</h:inputText>
<h:message for="age" style="color:red" />
</h:panelGrid>
<h:commandButton value="Submit" action="result" />
</h:form>
</h:body>
</html>
文件:result.xhtml 的代碼內(nèi)容如下所示 -
<?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>
Age : <h:outputText value="#{user.age}" />
</h:body>
</html>
文件:User.java 的代碼內(nèi)容如下所示 -
/*
* 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 {
int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
右鍵運行工程:ValidateIntRange,如果沒有任何錯誤,打開瀏覽器訪問:
http://localhost:8084/ValidateIntRange/
應(yīng)該會看到以下結(jié)果 -
簡單寫入一些信息(大于10),然后提交 -