seam 中应用自定义hibernate validator
关于自定义hibernate validation的方法网上一大把,就不多写了,给一个地址吧:http://xiaoyou8519.blog.163.com/blog/static/14015728620110614059638/
下面是我用到的一个自定义的验证:主要做的是验证一下卡号是不是唯一的
代码:
public class SameValidator implements Validator<Same>, PropertyConstraint,Serializable {private static final long serialVersionUID = -7858011546519554903L;@SuppressWarnings("unchecked")public void apply(Property property) {if (!(property.getPersistentClass() instanceof SingleTableSubclass)&& !(property.getValue() instanceof Collection)) {// single table should not be forced to nullif (!property.isComposite()) {Iterator<Column> iter = (Iterator<Column>) property.getColumnIterator();while (iter.hasNext()) {iter.next().setNullable(false);}}}}public void initialize(Same arg0) {}@SuppressWarnings("unchecked")public boolean isValid(Object obj) {if (obj == null) {return false;}if (obj instanceof String) {//添加时的验证逻辑 ...... //编辑时的验证逻辑 ......}return false;}}