in Advanced 

Resource preview: Know-how

HomeResource preview:
Log in

Passing parameters to JSF Validator using Jboss Seam

Hi,
Technologies used in our sample are Jboss SEAM + JSF + Facelets.
This post explains how to pass parameters to a jsf validator.

First we have to make our own facelet tag.
Create a file with name myvalidators.taglib.xml and put it in the class path at this location
/META-INF/myvalidators.taglib.xml and edit the file to be like this:
<?xml version="1.0"?>
<!DOCTYPE facelet-taglib PUBLIC
"-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN" "facelet-taglib_1_0.dtd">
<facelet-taglib>
    <namespace>http://example.com/jsf/my/validators</namespace>
    <tag>
        <tag-name>myValidator</tag-name>
        <validator>
            <validator-id>myValidator</validator-id>
        </validator>
    </tag>
</facelet-taglib>


Important thing is the file to be named "myvalidators.taglib.xml" and to be located inside "/META-INF/" folder somewhere in the class path in order facelets to discover it (autodiscover). This is called "well known location" for facelet libraries. Important thing is the suffix of the file to be ".taglib.xml".

Second thing is to make your own validator and mark it with the SEAM annotations. Write your own code to validate the input field. You can use your new field from the validator, as it should be automatically initialized.

package com.example.validators;

import org.jboss.seam.annotations.Name;
import org.jboss.seam.ScopeType;

import javax.faces.context.FacesContext;
import javax.faces.component.UIComponent;
import javax.faces.validator.ValidatorException;
import javax.faces.application.FacesMessage;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import javax.faces.component.UIComponent;
import java.io.Serializable;

@org.jboss.seam.annotations.faces.Validator(id = "myValidator")
@Name("myValidator")
public class MyCustomValidator implements Validator, Serializable {
    private int exampleProp;

    public void validate(FacesContext facesContext, UIComponent c, Object o) throws ValidatorException {
        if (...condition...) {
            throw new ValidatorException(new FacesMessage(...whatever...  ));
        }
    }
    public int getExampleProp() {
        return exampleProp;
    }

    public void setExampleProp(int exampleProp) {
        this.exampleProp = exampleProp;
    }



Annotating the validator with seam saves publishing it in the faces-config.xml file. Attributes passed in the xhtml page are autowired to your properties from the validator.

In the XHTML you can use the validator in this way:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:s="http://jboss.com/products/seam/taglib"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:a4j="http://richfaces.org/a4j"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:my="http://example.com/jsf/my/validators">

                <h:inputText id="someField" value="#{somebean.name}" 
                                   required="true">
                    <my:myValidator exampleProp="100"/>
                </h:inputText>

</ui:composition>


One remark - if you plan to use this validator multiple times in the page, it is better to mark it as a stateless, in order each time to use a new instance of the validator.

@Scope(ScopeType.STATELESS)  
@org.jboss.seam.annotations.faces.Validator(id = "myValidator")
@Name("myValidator")
public class MyCustomValidator implements Validator, Serializable ...     
Vote:
Comments: 0
Please vote! Your opinion matters!
If you want to share knowledge with the others, post a resource
Add resource
| Home | Hall of fame | Registration | Log in | Terms of service | Privacy policy | Help | Contacts | RSS |