Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2016-06-07 15:07:41
Original
1112 people have browsed it

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

	}

}
Copy after login

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

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

}
Copy after login
然后在taglib.xml中加入
<tag>
		<tag-name>validateNumber</tag-name>
		<validator>
			<validator-id>number</validator-id>
			<handler-class>com.xx.validator.NumberValidatorHandler</handler-class>
		</validator>
	</tag>
Copy after login

使用方法
<inputtext label="xxx" style="width:220px;" value="#{xx.xxValue}">
									<validatenumber precision="2" scale="3">
								</validatenumber></inputtext>
Copy after login






Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template