Custom convertor In jsf1.2/facelet1.x

Source: Internet
Author: User
Step 1: first define a validator to inherit the validator in jsf. If a status is required, you must implement the StateHolder interface packagecom. xx. web. validator; importjavax. faces. component. stateHolder; importjavax. faces. component. UIComponent; importjavax. faces. context. facesContext

Step 1: first define a validator to inherit the validator in jsf. If a status is required, you must implement the StateHolder interface package com. xx. web. validator; import javax. faces. component. stateHolder; import javax. faces. component. UIComponent; import javax. faces. context. facesContext

Step 1: Define A validator to inherit the validator in jsf. If a status is required, the StateHolder interface must be implemented.

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;} @ Overridepublic void validate (FacesContext context, UIComponent component, Object value) throws ValidatorException {// here is the specific verification logic} @ Overridepublic Object saveState (FacesContext context) {Object values [] = new Object [3]; values [0] = precision; values [1] = scale; values [2] = mustPositive; return (values );} @ Overridepublic void restoreState (FacesContext context, Object state) {Object values [] = (Object []) state; precision = (Integer) values [0]; scale = (Integer) values [1]; mustPositive = (Boolean) values [2] ;}@ Overridepublic boolean isTransient () {return false ;}@ Overridepublic void setTransient (boolean newTransientValue ){}}

Add the following code to the faces-confg.xml

   
  
   numberValidator
    
  
   com.xx.web.validator.NumberValidator
  
 

So far, validator can be used.

However, the preceding parameters cannot be added.

Because we use facelet, we need to define a tag of facelet.

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");}@Overrideprotected 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;}}
Add the following content to taglib. xml:

 
  
   validateNumber
  
  
   
    number
   
   
    com.xx.validator.NumberValidatorHandler
   
  
 

Usage

 
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.