jsf2.0 下,想自定义一个校验器,能接受页面送来的参数,但是百度看了很多自定义校验器,都是仅仅是继承 javax.faces.validator, 实现校验功能,这样无法接收页面传来的参数,谷歌了一下,也没找到例子,想了一下, 既然jsf 自定义的标签可以接收参数,仿照它写一个,肯定也可以,于是下载了myfaces 源码(本人用的是myfaces2.0),看他是怎么写的。就拿javax.faces.validator.LengthValidator 来说吧。 为了实现校验功能 ,定义了LengthValidator 类,继承了Validator, PartialStateHolder ,其中Validator 是为了实现校验功能,
PartialStateHolder 是为了保存页面状态, 然后还定义了一个 继承GenericMinMaxValidatorTag 的ValidateLengthTag ,用于,页面标签
LengthValidator
package javax.faces.validator;
import javax.faces.component.PartialStateHolder;
import javax.faces.component.StateHolder;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFJspProperty;
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFValidator;
@JSFValidator(
name="f:validateLength",
bodyContent="empty",
tagClass="org.apache.myfaces.taglib.core.ValidateLengthTag")
@JSFJspProperty(
name="binding",
returnType = "javax.faces.validator.LengthValidator",
longDesc = "A ValueExpression that evaluates to a LengthValidator.")
public class LengthValidator
implements Validator, PartialStateHolder
{
ValidateLengthTag
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.myfaces.taglib.core;
import org.apache.myfaces.convert.ConverterUtils;
import javax.faces.validator.LengthValidator;
import javax.faces.validator.Validator;
import javax.servlet.jsp.JspException;
/**
* @author Thomas Spiegl (latest modification by $Author: bommel $)
* @author Manfred Geiler
* @version $Revision: 1187700 $ $Date: 2011-10-22 07:19:37 -0500 (Sat, 22 Oct 2011) $
*/
public class ValidateLengthTag extends GenericMinMaxValidatorTag<Integer>
{
private static final long serialVersionUID = 4858632671998693059L;
private static final String VALIDATOR_ID = "javax.faces.Length";
定义了这2类后,myface 还在/src/META-INF 下standard-faces-config.xml 与myfaces-metadata.xml 注册了这个组件
myfaces-metadata.xml
<validator>
<modelId>myfaces-api</modelId>
<className>javax.faces.validator.LengthValidator</className>
<sourceClassName>javax.faces.validator.LengthValidator</sourceClassName>
<name>f:validateLength</name>
<validatorId>javax.faces.Length</validatorId>
<bodyContent>empty</bodyContent>
<tagClass>org.apache.myfaces.taglib.core.ValidateLengthTag</tagClass>
<generatedTagClass>true</generatedTagClass>
<desc>Creates a validator and associateds it with the nearest parent
UIComponent</desc>
<longDesc>
<![CDATA[
Creates a validator and associateds it with the nearest parent
UIComponent. When invoked, the validator ensures that values are
valid strings with a length that lies within the minimum and maximum
values specified.
Commonly associated with a h:inputText entity.
Unless otherwise specified, all attributes accept static values or EL expressions.
see Javadoc of <a href="http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
]]>
</longDesc>
<property>
<name>maximum</name>
<className>int</className>
<desc>The largest value that should be considered valid</desc>
<longDesc>The largest value that should be considered valid.</longDesc>
<deferredValueType>java.lang.Integer</deferredValueType>
</property>
<property>
<name>minimum</name>
<className>int</className>
<desc>The smallest value that should be considered valid</desc>
<longDesc>The smallest value that should be considered valid.</longDesc>
<deferredValueType>java.lang.Integer</deferredValueType>
</property>
<property>
<name>disabled</name>
<className>java.lang.Boolean</className>
<desc>no description</desc>
<faceletsOnly>true</faceletsOnly>
</property>
<property>
<name>for</name>
<className>java.lang.String</className>
<desc>no description</desc>
<faceletsOnly>true</faceletsOnly>
</property>
<property>
<name>binding</name>
<className>javax.faces.validator.LengthValidator</className>
<desc>A ValueExpression that evaluates to a LengthValidator.</desc>
<longDesc>A ValueExpression that evaluates to a LengthValidator.</longDesc>
<generated>false</generated>
</property>
</validator>
standard-faces-config.xml 下
<validator-id>javax.faces.Length</validator-id>
<validator-class>javax.faces.validator.LengthValidator</validator-class>
最后,myfaces还在CoreLibrary 中添加了这个校验器
package org.apache.myfaces.view.facelets.tag.jsf.core;
import javax.faces.component.UIParameter;
import javax.faces.component.UISelectItem;
import javax.faces.component.UISelectItems;
import javax.faces.component.UIViewParameter;
import javax.faces.convert.DateTimeConverter;
import javax.faces.convert.NumberConverter;
import javax.faces.validator.BeanValidator;
import javax.faces.validator.DoubleRangeValidator;
import javax.faces.validator.LengthValidator;
import javax.faces.validator.LengthValidatorXZM;
import javax.faces.validator.LongRangeValidator;
import javax.faces.validator.RegexValidator;
import javax.faces.validator.RequiredValidator;
import org.apache.myfaces.view.facelets.tag.AbstractTagLibrary;
/**
* For Tag details, see JSF Core <a target="_new"
* href="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/tlddocs/f/tld-summary.html">taglib documentation</a>.
*
* @author Jacob Hookom
* @version $Id: CoreLibrary.java 1187700 2011-10-22 12:19:37Z bommel $
*/
public final class CoreLibrary extends AbstractTagLibrary{ /<pre code_snippet_id="409043" snippet_file_name="blog_20140628_6_9206090" name="code" class="java"><span style="font-family: Arial, Helvetica, sans-serif;"> this.addValidator("validateLength", LengthValidator.VALIDATOR_ID);</span>
其实我们在 myfaces-metadata.xml ,myfaces-metadata.xml 中定义自己的校验器 然后在CoreLibrary 中注册