SomeValidator.java
import javax.el.ValueExpression; import javax.faces.application.FacesMessage; 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 SomeValidator implements Validator, Stateholder { private ValueExpression type; private boolean transientValue; public ValueExpression getType() { return type; } public void setType(ValueExpression type) { this.type = type; } @Override public Object saveState(FacesContext context) { Object values[] = new Object[1]; values[0] = type; return values; } @Override public void restoreState(FacesContext context, Object state) { Object[] values = (Object[])state; type = (ValueExpression)values[0]; } @Override public boolean isTransient() { return transientValue; } @Override public void setTransient(boolean transientValue) { this.transientValue = transientValue; } @Override public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { ... } }
SomeValidatorHandler.java
public class SomeValidatorHandler extends ValidateHandler { private TagAttribute type; public SomeValidatorHandler(ValidatorConfig config) { super(config); type = getRequiredAttribute("type"); } @Override protected Validator createValidator(FaceletContext context) { SomeValidator validator = (SomeValidator)context.getFacesContext().getApplication().createValidator( "intersult.some"); if (type != null) validator.setType(type.getValueExpression(context, QueryType.class)); return validator; } @Override protected MetaRuleset createMetaRuleset(Class type) { return super.createMetaRuleset(type).ignoreAll(); } }
faces-config.xml
<validator> <validator-id>intersult.some</validator-id> <validator-class>com.intersult.SomeValidator</validator-class> </validator>
intersult.taglib.xml
<tag> <tag-name>SomeValidator</tag-name> <validator> <validator-id>intersult.some</validator-id> <handler-class>com.intersult.SomeValidatorHandler</handler-class> </validator> </tag>