开发者

Is there any configuration necessary to use Hibernate Validation custom messages

I'm using the following class to validate my beans with Hibernate Validation. It works great. It's basically the listener found here for JPA 1, converted to use to validate beans in my Spring controllers. I'm attempting to customize the messages returned. This and every other resource I can find say to just put a ValidationMessages.properties file in the Web-Inf directory. This file is also included below. This is not working. Any suggestions?

public class BeanValidationTool {

    public void validate(Object entity) {
        TraversableResolver tr = new MyTraversableResolver();
      开发者_如何学运维  Validator validator = Validation.buildDefaultValidatorFactory().usingContext().traversableResolver(tr).getValidator();
        final Set<ConstraintViolation<Object>> constraintViolations = validator.validate(entity);
        if (constraintViolations.size() > 0) {
          Set<ConstraintViolation<?>> propagatedViolations = new HashSet<ConstraintViolation<?>>(constraintViolations.size());
          Set<String> classNames = new HashSet<String>();
            for (ConstraintViolation<?> violation : constraintViolations) {
              propagatedViolations.add(violation);
              classNames.add(violation.getLeafBean().getClass().getName());
            }
            StringBuilder builder = new StringBuilder();
            builder.append("validation failed for classes ");
            builder.append(classNames);
            throw new ConstraintViolationException(builder.toString(), propagatedViolations);
        }
    }

    public class MyTraversableResolver  implements TraversableResolver {

        public boolean isReachable(Object traversableObject, Path.Node traversableProperty, Class<?> rootBeanType, Path pathToTraversableObject, ElementType elementType) {
            return traversableObject == null || Hibernate.isInitialized(traversableObject);
        }

        public boolean isCascadable(Object traversableObject, Path.Node traversableProperty, Class<?> rootBeanType, Path pathToTraversableObject, ElementType elementType) {
            return true;
        }
    }
  }`

ValidationMessages.properties file

validator.min = must be greater than or equal to {value}

validator.notEmpty = This field can't be empty


You have to define messageSource in your application context:

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" abstract="false"
      scope="singleton" lazy-init="default">
    <property name="basename" value="ValidationMessages"/>
</bean>

Than you just use the bean to retrieve the messages:

@Autowired
private MessageSource messageSource;

public String getMessage(String messageName) {
    return messageSource.getMessage(messageName, null, null);
}

In order to access the beans in context you will have to do this as well:

@Component
public class BeanValidationTool{...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜