第一步,先定义一个validator继承jsf中的validator,如果需要有状态就必须实现StateHolder接口 package com.xx.web.validator;import javax.faces.component.StateHolder;import javax.faces.component.UIComponent;import javax.faces.context.FacesContext
第一步,先定义一个validator继承jsf中的validator,如果需要有状态就必须实现StateHolder接口
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; } @Override public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { <span style="white-space:pre"> </span>//这里是具体验证的逻辑 } @Override public Object saveState(FacesContext context) { Object values[] = new Object[3]; values[0] = precision; values[1] = scale; values[2]= mustPositive; return (values); } @Override public void restoreState(FacesContext context, Object state) { Object values[] = (Object[]) state; precision = (Integer) values[0]; scale = (Integer) values[1]; mustPositive=(Boolean)values[2]; } @Override public boolean isTransient() { return false; } @Override public void setTransient(boolean newTransientValue) { } }
<validator> <validator-id>numberValidator</validator-id> <validator-class>com.xx.web.validator.NumberValidator</validator-class> </validator>
但是上面的参数无法添加
因为我们使用了facelet,所以要定义一个facelet的tag
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"); } @Override protected 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; } }
<tag> <tag-name>validateNumber</tag-name> <validator> <validator-id>number</validator-id> <handler-class>com.xx.validator.NumberValidatorHandler</handler-class> </validator> </tag>
<inputtext label="xxx" style="width:220px;" value="#{xx.xxValue}"> <validatenumber precision="2" scale="3"> </validatenumber></inputtext>