首頁  >  文章  >  資料庫  >  jsf1.2/facelet1.x 中convertor的自定义

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

WBOY
WBOY原創
2016-06-07 15:07:411116瀏覽

第一步,先定义一个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) {

	}

}

在faces-confg.xml中添加以下代码
<validator>
  <validator-id>numberValidator</validator-id>
  <validator-class>com.xx.web.validator.NumberValidator</validator-class>
</validator>

到此为止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中加入
<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>






陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn