如何在spring mvc validation中禁用消息中的花括号替换

时间:2014-08-12 13:47:46

标签: java spring validation spring-mvc bean-validation

我有@Pattern注释的字段

最终公共类科{

@Pattern( regexp = "([0-9]{1}[1-9]{1}|[1-9]{1}[0-9]{1})[0-9]{8}", message = "Inn should be validated by pattern \"{regexp}\". Check branch settings" )
private String INN;

}

以弹簧形式:错误标记

   <form:errors path="*"/>

如果我输入了错误的INN字段,我收到了违规消息

Inn should be validated by pattern ([0-9][Ljavax.validation.constraints.Pattern$Flag;@4a5b4234[1-9][Ljavax.validation.constraints.Pattern$Flag;@4a5b4234|[1-9][Ljavax.validation.constraints.Pattern$Flag;@4a5b4234[0-9][Ljavax.validation.constraints.Pattern$Flag;@4a5b4234)[0-9]{8}.. Check branch settings

Spring将regexp中的花括号替换为其他对象。 例如,{1}被Ljavax.validation.constraints.Pattern $ Flag取代; @ 4a5b4234 如何禁用这种恼人的行为?

1 个答案:

答案 0 :(得分:1)

您需要使用简单的引号转义{regexp}部分(\"'{regexp}'\")以使Spring忽略它:

@Pattern(regexp = "([0-9]{1}[1-9]{1}|[1-9]{1}[0-9]{1})[0-9]{8}", message = "Inn should be validated by pattern \"'{regexp}'\". Check branch settings")