Step 1: first define a validator to inherit the validator in jsf. If a status is required, you must implement the StateHolder interface packagecom. xx. web. validator; importjavax. faces. component. stateHolder; importjavax. faces. component. UIComponent; importjavax. faces. context. facesContext
Step 1: first define a validator to inherit the validator in jsf. If a status is required, you must implement the StateHolder interface package com. xx. web. validator; import javax. faces. component. stateHolder; import javax. faces. component. UIComponent; import javax. faces. context. facesContext
Step 1: Define A validator to inherit the validator in jsf. If a status is required, the StateHolder interface must be implemented.
Package com. xx. web. validator; import javax. faces. component. stateHolder; import javax. faces. component. UIComponent; import javax. faces. context. facesContext; import javax. faces. validator. validator; import javax. faces. validator. validatorException; public class NumberValidator implements Validator, StateHolder {private int precision = 10; private int scale = 6; private boolean mustPositive = true; public NumberValidator () {} public void setPrecision (int precision) {this. precision = precision;} public void setScale (int scale) {this. scale = scale;} public void setMustPositive (Boolean mustPositive) {this. mustPositive = mustPositive;} @ Overridepublic void validate (FacesContext context, UIComponent component, Object value) throws ValidatorException {// here is the specific verification logic} @ Overridepublic Object saveState (FacesContext context) {Object values [] = new Object [3]; values [0] = precision; values [1] = scale; values [2] = mustPositive; return (values );} @ Overridepublic void restoreState (FacesContext context, Object state) {Object values [] = (Object []) state; precision = (Integer) values [0]; scale = (Integer) values [1]; mustPositive = (Boolean) values [2] ;}@ Overridepublic boolean isTransient () {return false ;}@ Overridepublic void setTransient (boolean newTransientValue ){}}
Add the following code to the faces-confg.xml
numberValidator
com.xx.web.validator.NumberValidator
So far, validator can be used.
However, the preceding parameters cannot be added.
Because we use facelet, we need to define a tag of facelet.
package com.xx.web.validator;import javax.faces.validator.Validator;import com.sun.facelets.FaceletContext;import com.sun.facelets.tag.TagAttribute;import com.sun.facelets.tag.jsf.ValidateHandler;import com.sun.facelets.tag.jsf.ValidatorConfig;public class NumberValidatorHandler extends ValidateHandler {private final TagAttribute precisionAttr;private final TagAttribute scaleAttr;private final TagAttribute mustPositiveAttr;public NumberValidatorHandler(ValidatorConfig config) {super(config);precisionAttr = this.getAttribute("precision");scaleAttr = this.getAttribute("scale");mustPositiveAttr = this.getAttribute("mustPositive");}@Overrideprotected Validator createValidator(FaceletContext ctx) {NumberValidator result = (NumberValidator) ctx.getFacesContext().getApplication().createValidator("number");if (precisionAttr != null)result.setPrecision(Integer.valueOf(precisionAttr.getValue(ctx)));if (scaleAttr != null)result.setScale(Integer.valueOf(scaleAttr.getValue(ctx)));if (mustPositiveAttr != null)result.setMustPositive(Boolean.valueOf(mustPositiveAttr.getValue(ctx)));return result;}}
Add the following content to taglib. xml:
validateNumber
number
com.xx.validator.NumberValidatorHandler
Usage