Frage

I am using spring 3.0.2 for developing a simple web based application like twitter. My server side validations are going good with JSR validation API, but i can't find any ways to internationalized messages in JSR validation API upon failed validation.

I am using following annotation on my model class Spitter.java representing a user in application.

public class Spitter implements Serializable{

private Long id;


@Size(min=8, max=15, message="some msg..")
@Pattern(regexp="^[a-zA-Z0-9]+$", message="some msg..")
private String username;

@Size(min=8, max=15, message="some msg..")
private String password;

@Size(min=3, max=15, message="some msg..")
private String name;

@Pattern(regexp="^$",message="some msg..")
private String email;

@NotNull(message="some msg..")
@NumberFormat(style= NumberFormat.Style.NUMBER)
@Min(value=14, message="some msg..")
@Max(value=99, message="some msg..")
private Integer age;

//getter setter not shown

}

I have checked JSR Validation API for localization support upon failed validations, but unable to find the same.

Is there any way i can acheive internationalization.

Thanks in advance...

War es hilfreich?

Lösung

You can put a ValidationMessages.properties file in your classpath with the approrpriately customized messages. You an either override the current set of messages, say for eg:

javax.validation.constraints.NotNull.message=may not be null

or if you want to use your own custom message:

   @NotNull(message="{custom.message}")

an entry for which should be present in the ValidationMessages.properties file.

This information is present in section 4.3.1.1. of JSR 303 specs

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top