跳至主要内容

JSF 2.3: Class level validation with f:validateWholeBean

Class level validation with f:validateWholeBean

JSF provides a f:validateBean which bridges Bean Validation to JSF, why we need another f:validateWholeBean?
This is explained in details in the VDL docs of f:validateWholeBean.
Support multi-field validation by enabling class-level bean validation on CDI based backing beans. This feature causes a temporary copy of the bean referenced by the value attribute, for the sole purpose of populating the bean with field values already validated by <f:validateBean /> and then performing class-level validation on the copy. Regardless of the result of the class-level validation, the copy is discarded.
in another word, it provides the capability of cross fields validation.
A good example is password matching check in a signup page, we have to make sure the values in password field and password confirm field are equivalent.
Create a bean validation annotation @Password.
@Constraint(validatedBy=PasswordValidator.class)
@Target(TYPE)
@Retention(RUNTIME)
@interface Password {

    String message() default "Password fields must match";
    Class[] groups() default {};
    Class[] payload() default {};
}
Constraint declares which valiator will handle this annotation, here it is PasswordValidator.
public class PasswordValidator implements ConstraintValidator<Password, PasswordHolder> {

  @Override
  public void initialize(Password constraintAnnotation) { }

  @Override
  public boolean isValid(PasswordHolder value, ConstraintValidatorContext context) {
    boolean result;
    
    result = value.getPassword1().equals(value.getPassword2());

    return result;
  }

}
ConstraintValidator accept two parameters, the validator annotation, and the type(PasswordHolder) will be applied.
PasswordHolder is an interface which holds the values of two password fields.
public interface PasswordHolder {
    
    String getPassword1();
    String getPassword2();  
}
Create a backing bean to put all together.
@Named
@RequestScoped
@Password(groups = PasswordValidationGroup.class)
public class BackingBean implements PasswordHolder, Cloneable {

    private String password1;

    private String password2;

    public BackingBean() {
        password1 = "";
        password2 = "";
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        BackingBean other = (BackingBean) super.clone();
        other.setPassword1(this.getPassword1());
        other.setPassword2(this.getPassword2());
        return other;
    }

    @NotNull(groups = PasswordValidationGroup.class)
    @Size(max = 16, min = 8, message = "Password must be between 8 and 16 characters long",
            groups = PasswordValidationGroup.class)
    @Override
    public String getPassword1() {
        return password1;
    }

    public void setPassword1(String password1) {
        this.password1 = password1;
    }

    @NotNull(groups = PasswordValidationGroup.class)
    @Size(max = 16, min = 8, message = "Password must be between 8 and 16 characters long",
            groups = PasswordValidationGroup.class)
    @Override
    public String getPassword2() {
        return password2;
    }

    public void setPassword2(String password2) {
        this.password2 = password2;
    }

}
We apply Password validation on the backingBean, it is a class level validation.
Create a simple facelets template.
<h:panelGroup id="messageFromInputBox">
 <h:messages />
</h:panelGroup>
<h:form>
 <h:panelGrid columns="2">

  <h:outputText value="Password" />  
  <h:inputSecret id="password1" value='#{backingBean.password1}' required="true">
   <f:validateBean validationGroups="javax.validation.groups.Default,com.hantsylabs.example.ee8.jsf.PasswordValidationGroup" />
  </h:inputSecret>

  <h:outputText value="Password again" /> 
  <h:inputSecret id="password2" value='#{backingBean.password2}' rendered="true">
   <f:validateBean validationGroups="javax.validation.groups.Default,com.hantsylabs.example.ee8.jsf.PasswordValidationGroup" />
  </h:inputSecret>

 </h:panelGrid>

 <f:validateWholeBean value='#{backingBean}' 
       validationGroups="com.hantsylabs.example.ee8.jsf.PasswordValidationGroup" />
 <div>
  <h:commandButton 
   id="validtePassword" 
   value="Validate Password">
   <f:ajax execute="@form" render=":messageFromInputBox" />
  </h:commandButton>
 </div>
</h:form>
f:validateWholeBean accept a value attribute to set the backing bean.
NOTE, we use a PasswordValidationGroup group to differentiate varied validations.
PasswordValidationGroup is just an interface.
public interface PasswordValidationGroup {
    
}
Run the project on Glassfish v5, open your browser and navigate to http://localhost:8080/jsf-validwholebean/.
Try to input passwords, if its length is less than 8, submit form, it will display erros like.
length validation error
If the length is valid, and the two password are not matched, it will trigger the @Password validator.
password validator error
Grab the source codes from my github account, and have a try.

评论

此博客中的热门博文

Create a restful application with AngularJS and Zend 2 framework

Create a restful application with AngularJS and Zend 2 framework This example application uses AngularJS/Bootstrap as frontend and Zend2 Framework as REST API producer. The backend code This backend code reuses the database scheme and codes of the official Zend Tutorial, and REST API support is also from the Zend community. Getting Started with Zend Framework 2 Getting Started with REST and Zend Framework 2 Zend2 provides a   AbstractRestfulController   for RESR API producing. class AlbumController extends AbstractRestfulController { public function getList() { $results = $this->getAlbumTable()->fetchAll(); $data = array(); foreach ($results as $result) { $data[] = $result; } return new JsonModel(array( 'data' => $data) ); } public function get($id) { $album = $this->getAlbumTable()->getAlbum($id); return new JsonModel(array("data" =>

JPA 2.1: Attribute Converter

JPA 2.1: Attribute Converter If you are using Hibernate, and want a customized type is supported in your Entity class, you could have to write a custom Hibernate Type. JPA 2.1 brings a new feature named attribute converter, which can help you convert your custom class type to JPA supported type. Create an Entity Reuse the   Post   entity class as example. @Entity @Table(name="POSTS") public class Post implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name="ID") private Long id; @Column(name="TITLE") private String title; @Column(name="BODY") private String body; @Temporal(javax.persistence.TemporalType.DATE) @Column(name="CREATED") private Date created; @Column(name="TAGS") private List<String> tags=new ArrayList<>(); } Create an attribute convert

Auditing with Hibernate Envers

Auditing with Hibernate Envers The approaches provided in JPA lifecyle hook and Spring Data auditing only track the creation and last modification info of an Entity, but all the modification history are not tracked. Hibernate Envers fills the blank table. Since Hibernate 3.5, Envers is part of Hibernate core project. Configuration Configure Hibernate Envers in your project is very simple, just need to add   hibernate-envers   as project dependency. <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-envers</artifactId> </dependency> Done. No need extra Event listeners configuration as the early version. Basic Usage Hibernate Envers provides a simple   @Audited   annotation, you can place it on an Entity class or property of an Entity. @Audited private String description; If   @Audited   annotation is placed on a property, this property can be tracked. @Entity @Audited public class Signup implements Serializa