简体   繁体   English

如何动态设置@Validate批注的mask选项的值

[英]How to dynamically set value of mask option of @Validate annotation

Here's the code: 这是代码:

SomeFile.properties SomeFile.properties

string.regex="someRegex"

SomeActionClass.java SomeActionClass.java

@Value("${string.regex}")
private String REGEX="";

@Validate(mask = REGEX)
private String stringToValidate;

The file SomeFile.properties is loaded on server startup and the value "someRegex" is stored somewhere in the session, so it is available. 服务器启动时将加载文件SomeFile.properties,并且值“ someRegex”存储在会话中的某个位置,因此该文件可用。

The problem is that mask requires a constant value, and if I put final at REGEX="" I cannot overwrite the init value with the string.regex one; 问题是掩码需要一个恒定值,如果我将final放在REGEX =“”处,则无法用string.regex一个覆盖初始值。 on the other hand if I put final but do not initialize the variable, java compiler complain about it. 另一方面,如果我将final变量放入但不初始化变量,则Java编译器会抱怨它。

Is there a way to use mask option with a value retrieved from file that I do not know? 有没有一种方法可以将mask选项与从我不知道的文件中检索的值一起使用?

Is it possible to do this directly on a jsp in a smart way, since I've have several fields to validate and I do not want to perform a check and write a message error for each one? 因为我有几个要验证的字段并且我不想执行检查并为每个错误编写消息错误,是否可以以一种灵巧的方式直接在jsp上执行此操作?

Annotation value can be changed at runtime using reflection. 注释值可以在运行时使用反射进行更改。

Please take a look at this (see the post marked as answer) 请对此进行查看(请参阅标记为答案的帖子)

Modify a class definition's annotation string parameter at runtime 在运行时修改类定义的注释字符串参数

Be careful with this approach, some of the caveats of it are also described. 请谨慎使用此方法,并说明其中的一些注意事项。 Also, for being able to chenge this value at runtime, the retention policy for the annotation has to be Runtime 另外,为了能够在运行时调整该值,注释的保留策略必须是“ Runtime

It's not the solution to your particular question, but it has useful information on how ti perform this operation on annotations. 这不是解决您特定问题的方法,但是它具有有关如何对注释执行此操作的有用信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM