JSF Tip #9 - The BeanValidator

Since JSF 2 it is also possible to use BeanValidation as specified in JSR 303. The following blog article describes how this JSR has been integrated within JSF 2.

With the following JSF managed bean.


    public class UserInfo {
        /**
         * Stores the username.
         */
        private String username;

        /**
         * Get the username.
         *
         * @return the username.
         */
        @NotNull
        public String getUsername() { 
            return username; 
        }

        /**
         * Set the username.
         *
         * @param username the username.
         */
        public void setUsername(String username) { 
            this.username = username; 
        }
    }
        

And the following JSF page.


    <h:form>
        <f:validateBean>
            <h:inputText id="username" value="#{userInfo.username}"/>
        </f:validateBean>
    </h:form>
        

See http://beanvalidation.org/ for more information about Bean Validation.

Posted September 17, 2012

Up