欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

jsf1.2/facelet1.x 中convertor的自定义

程序员文章站 2022-03-31 17:32:20
...

第一步,先定义一个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 {
		//这里是具体验证的逻辑
	}

	@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) {

	}

}

在faces-confg.xml中添加以下代码
numberValidatorcom.xx.web.validator.NumberValidator

到此为止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;
	}

}
然后在taglib.xml中加入
validateNumbernumbercom.xx.validator.NumberValidatorHandler

使用方法