Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change location of ValidationMessages.properties in Bean Validation

By default, ValidationMessages.properties can be located in root of my classpath. Can I change the location of this file like com.myapp.Anything.properties?

like image 765
Otávio Garcia Avatar asked Jun 09 '11 13:06

Otávio Garcia


People also ask

What is a message element in Java Bean Validation?

Java Bean validation specifications require that each constraint annotation must include a 'message' element definition. The message element value represents validation error message. This element usage is optional on client side. If not specified a default message resource key is resolved and displayed.

How to modify the default behavior of Java Bean Validation?

This is the default behavior which can be modified by implementing javax.validation.MessageInterpolator Java Bean Validation specification supports Unified Expression Language (JSR 341) for evaluating dynamic expression in the validation messages that's the reason we have to include 'javax.el' dependency in our projects.

How to validate the name of a bean property?

In such a case, you may want to use the validateProperty () method, which is called with a constrained object and the name of the bean property to be validated: Here, a constraint violation will be raised because an empty string has been passed as a value for the name field.

How to evaluate dynamic expression in Java Bean Validation?

Java Bean Validation specification supports Unified Expression Language (JSR 341) for evaluating dynamic expression in the validation messages that's the reason we have to include 'javax.el' dependency in our projects. Within the messages, constraint annotation element values can be referenced as ' {theElementName}'.


1 Answers

From the JSR-303 specification:

4.3.1.1. Default message interpolation algorithm The default message interpolator uses the following steps:

  1. Message parameters are extracted from the message string and used as keys to search the ResourceBundle named ValidationMessages (often materialized as the property file /ValidationMessages.properties and its locale variations) using the defined locale (see below). If a property is found, the message parameter is re- placed with the property value in the message string. Step 1 is applied recursively until no replacement is per- formed (i.e. a message parameter value can itself contain a message parameter).

It seems that this is the default and the suggestion. To back this up the JSR-303 TCK uses TestNG to move them to the "WEB-INF/classes/" directory which is the same as "./" on the resources classpath.

(You can experiment with it but that's about all I can figure out.)

Sorry.

like image 147
Chris Ruffalo Avatar answered Oct 12 '22 03:10

Chris Ruffalo