<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>新建网页 1</title>
</head>
<body>
<p>自订转换, 验证标签</p>
<div id="PageContent">
<table cellSpacing="0" cellPadding="0" width="100%" border="0" id="table3">
<tr>
<td class="pagebody" vAlign="top">
<table style="CLEAR: both" cellSpacing="0" cellPadding="0" width="100%" border="0" id="table4">
<tr>
<td class="pagecontent" vAlign="top" width="100%">
<div class="wiki-content">
在 <span class="nobr">
<a title="Visit page outside Confluence" rel="nofollow" href="http://www.javaworld.com.tw/confluence/pages/viewpage.action?pageId=2694">
自订验证器<sup><img class="rendericon" height="7" alt="" src="http://www.javaworld.com.tw/confluence/images/icons/linkext7.gif" width="7" align="absMiddle" border="0"></sup></a></span>
中,我们的验证器只能验证一种pattern(.+[0-9]+),我们希望可以在JSF页面上自订匹配的pattern,然而由于我们使用<f:
validator>这个通用的验证器标签,为了要能提供pattern属性,我们可以使用<f:attribute>标签来设置,例如:<div class="code" style="BORDER-TOP-STYLE: solid; BORDER-RIGHT-STYLE: solid; BORDER-LEFT-STYLE: solid; BORDER-BOTTOM-STYLE: solid">
<div class="codeContent">
<pre class="code-java">....
<h:inputSecret value=<span class="code-quote">"#{user.password}"</span> required=<span class="code-quote">"<span class="code-keyword">true</span>"</span>>
<f:validator validatorId=<span class="code-quote">"onlyfun.caterpillar.Password"</span>/>
<f:attribute name=<span class="code-quote">"pattern"</span> value=<span class="code-quote">".+[0-9]+"</span>/>
</h:inputSecret><p>
....</pre>
</div>
</div>
<p> 使用<f:attribute>卷标来设定属性,接着我们可以如下取得所设定的属性:</p>
<div class="code" style="BORDER-TOP-STYLE: solid; BORDER-RIGHT-STYLE: solid; BORDER-LEFT-STYLE: solid; BORDER-BOTTOM-STYLE: solid">
<div class="codeContent">
<pre class="code-java">....
<span class="code-keyword">public</span> void validate(FacesContext context,
UIComponent component,
<span class="code-object">Object</span> obj)
<span class="code-keyword">throws</span> ValidatorException {
....
<span class="code-object">String</span> pattern = (<span class="code-object">String</span>)
component.getAttributes().get(<span class="code-quote">"pattern"</span>);
....
}
....</pre>
</div>
</div>
<p> 您也可以开发自己的一组验证卷标,并提供相关属性设定,这需要了解JSP Tag
Library的撰写,所以请您先参考 <span class="nobr">
<a title="Visit page outside Confluence" rel="nofollow" href="http://www.javaworld.com.tw/confluence/pages/viewpage.action?pageId=869">
JSP/Servlet<sup><img class="rendericon" height="7" alt="" src="http://www.javaworld.com.tw/confluence/images/icons/linkext7.gif" width="7" align="absMiddle" border="0"></sup></a></span>
中有关于JSP Tag Library的介绍。</p>
<p>
要开发验证器转用标签,您可以直接继承javax.faces.webapp.ValidatorTag,这个类别可以帮您处理大部份的细节,您所需要的,就是重新定义它的createValidator()方法,我们以改写
<span class="nobr">
<a title="Visit page outside Confluence" rel="nofollow" href="http://www.javaworld.com.tw/confluence/pages/viewpage.action?pageId=2694">
自订验证器<sup><img class="rendericon" height="7" alt="" src="http://www.javaworld.com.tw/confluence/images/icons/linkext7.gif" width="7" align="absMiddle" border="0"></sup></a></span>
中的PasswordValidator为例:</p>
<ul>
<li>PasswordValidator.java </li>
</ul>
<div class="code" style="BORDER-TOP-STYLE: solid; BORDER-RIGHT-STYLE: solid; BORDER-LEFT-STYLE: solid; BORDER-BOTTOM-STYLE: solid">
<div class="codeHeader" style="BORDER-BOTTOM-STYLE: solid">
<b>PasswordValidator.java</b></div>
<div class="codeContent">
<pre class="code-java"><span class="code-keyword">package</span> onlyfun.caterpillar;
<span class="code-keyword">import</span> javax.faces.application.FacesMessage;
<span class="code-keyword">import</span> javax.faces.component.UIComponent;
<span class="code-keyword">import</span> javax.faces.context.FacesContext;
<span class="code-keyword">import</span> javax.faces.validator.Validator;
<span class="code-keyword">import</span> javax.faces.validator.ValidatorException;
<span class="code-keyword">public</span> class PasswordValidator <span class="code-keyword">implements</span> Validator {
<span class="code-keyword">private</span> <span class="code-object">String</span> pattern;
<span class="code-keyword">public</span> void setPattern(<span class="code-object">String</span> pattern) {
<span class="code-keyword">this</span>.pattern = pattern;
}
<span class="code-keyword">public</span> void validate(FacesContext context,
UIComponent component,
<span class="code-object">Object</span> obj)
<span class="code-keyword">throws</span> ValidatorException {
<span class="code-object">String</span> password = (<span class="code-object">String</span>) obj;
<span class="code-keyword">if</span>(password.length() < 6) {
FacesMessage message = <span class="code-keyword">new</span> FacesMessage(
FacesMessage.SEVERITY_ERROR,
<span class="code-quote">"字符长度小于6"</span>, <span class="code-quote">"字符长度不得小于6"</span>);
<span class="code-keyword">throw</span> <span class="code-keyword">new</span> ValidatorException(message);
}
<span class="code-keyword">if</span>(pattern != <span class="code-keyword">null</span> && !password.matches(pattern)) {
FacesMessage message = <span class="code-keyword">new</span> FacesMessage(
FacesMessage.SEVERITY_ERROR,
<span class="code-quote">"密码必须包括字符与数字"</span>,
<span class="code-quote">"密码必须是字符加数字所组成"</span>);
<span class="code-keyword">throw</span> <span class="code-keyword">new</span> ValidatorException(message);
}
}
}</pre>
</div>
</div>
<p>
主要的差别是我们提供了pattern属性,在validate()方法中进行验证时,是根据我们所设定的pattern属性,接着我们继承javax.faces.webapp.ValidatorTag来撰写自己的验证标签:</p>
<div class="code" style="BORDER-TOP-STYLE: solid; BORDER-RIGHT-STYLE: solid; BORDER-LEFT-STYLE: solid; BORDER-BOTTOM-STYLE: solid">
<div class="codeHeader" style="BORDER-BOTTOM-STYLE: solid">
<b>PasswordValidatorTag.java</b></div>
<div class="codeContent">
<pre class="code-java"><span class="code-keyword">package</span> onlyfun.caterpillar;
<span class="code-keyword">import</span> javax.faces.application.Application;
<span class="code-keyword">import</span> javax.faces.context.FacesContext;
<span class="code-keyword">import</span> javax.faces.validator.Validator;
<span class="code-keyword">import</span> javax.faces.webapp.ValidatorTag;
<span class="code-keyword">public</span> class PasswordValidatorTag <span class="code-keyword">extends</span> ValidatorTag {
<span class="code-keyword">private</span> <span class="code-object">String</span> pattern;
<span class="c