标准验证器
index.jsp
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@page contentType="text/html;charset=Big5"%>
<html>
<head>
<title>验证器示范</title>
</head>
<body>
<f:view>
<h:messages layout="table" style="color:red"/>
<h:form>
<h3>请输入您的名称</h3>
<h:outputText value="#{user.errMessage}"/><p>
名称: <h:inputText value="#{user.name}"
required="true"/><p>
密码: <h:inputSecret value="#{user.password}"
required="true">
<f:validateLength minimum="6"/>
</h:inputSecret><p>
<h:commandButton value="送出"
action="#{user.verify}"/>
</h:form>
</f:view>
</body>
</html>
在<h:inputText>、</h:inputSecret>中,我们设定了required属性为true,这表示这个字段一定要输入值,我们也在</h:inputSecret>设定了<f: validateLength>,并设定其minimum属性为6,这表示这个字段最少需要6个字符。
这一次在错误讯息的显示上,我们使用<h:messages>标签,当有验证错误发生时,相关的错误讯息会收集起来,使用<h:messages>卷标可以一次将所有的错误讯息显示出来。
JSF提供了三种标准验证器:<f:validateDoubleRange>、<f:validateLongRange>、<f:validateLength>,您可以分别查询它们的 Tag Library Documentation,了解他们有哪些属性可以使用,或者是参考 Using the Standard Validators 这篇文章中有关于标准验证器的说明。
参考文献 《JSF入门》简体中文版