Tag Archives: binding

Using binding in JSF

You may heard about binding term in JSF. But it can be used?
Binding can be involved in validation procedure.

1. Add form to JSF page:

    <h:inputText id="email" 
        binding="#{SomeBaen.emailComponent}" />
    <h:message for="email" />

    <h:commandButton value="Check" action="#{SomeBaen.doCheck}" />

2. Create bean:

public class SomeBean implements Serializable {
    private static final long serialVersionUID = 1L;
    private String emailValue;
    private UIComponent emailComponent;
    public String doCheck() {
        if (isValid(emailValue)) {
            return "ok";
        // set invalid message
        FacesMessage message = new FacesMessage("Invalid email");
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(emailComponent.getClientId(context), message);
        return null;
    public void setEmailValue(String emailValue) {
        this.emailValue = emailValue;
    public String getEmailValue() {
        return emailValue;
    public void setEmailComponent(UIComponent emailComponent) {
        this.emailComponent = emailComponent;
    public UIComponent getEmailComponent() {
        return emailComponent;

So, if validation failed you’ll see a message near inputText control.
Also example http://stackoverflow.com/a/321722