显示 struts 验证消息
我想显示在操作类中检测到的错误,我使用:
errors.add(ActionErrors.GLOBAL_MESSAGE,
new ActionMessage("some_string_in_properties_file"));`
并且它工作正常。但是,我已经编写了一些通用错误消息,并且我想重用它们,因此我尝试这样做:
errors.add(ActionErrors.GLOBAL_MESSAGE,
new ActionMessage("string1_in_properties_file", "string2_in_properties_file"));
其中 string1 =
。
然后它显示 string2 is required
。它不会用 string2 的值替换它。
我什至尝试过
errors.add(ActionErrors.GLOBAL_MESSAGE,
new ActionMessage("string1_in_properties_file",
new ActionMessage("string2_in_properties_file")));
,然后它显示 string2[] is required
。它不会替换 string2。
我知道可以通过硬编码值来完成,但是还有其他方法吗?
I want to display errors detected in an action class, I use:
errors.add(ActionErrors.GLOBAL_MESSAGE,
new ActionMessage("some_string_in_properties_file"));`
and it works fine. However, I have written some generic error messages, and I would like to reuse them, so I am trying to do this:
errors.add(ActionErrors.GLOBAL_MESSAGE,
new ActionMessage("string1_in_properties_file", "string2_in_properties_file"));
where string1 = <li>{0} is required.</li>
.
Then it is displaying string2 is required
. It is not replacing string2 with its value.
I even tried
errors.add(ActionErrors.GLOBAL_MESSAGE,
new ActionMessage("string1_in_properties_file",
new ActionMessage("string2_in_properties_file")));
then it is displaying string2[] is required
. It is not replacing string2.
I know it can be done by hard-coding the value, but is there any other way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于您想从属性文件中获取两个键的值,并将其放入全局错误键中,
我想说,使用单独检索每个值
,然后将其放入全局错误中
希望它有帮助......
Since you want to to fetch two key's value from Property file, and put it in global error key,
I would say, retrieve each value separately using
and then put it in your global error
Hope it help....
很难准确地告诉您要做什么,因为 O 不知道
errors
和ActionMessage
背后的代码。但是您可以使用String.format
。您的代码看起来像这样,
上面将打印
It's hard to tell you exactly what to do, since O don't know the code behind
errors
andActionMessage
. But you can, however, useString.format
. Your code would look something like thisand
The above will print
在Struts中 ActionMessage ,您可以指定参数值
{0}
、{1}
、{2}
、{3}
> 在属性文件中指定,如下所示:或者:
value1
..value3
可以是任何类型(因为 Struts 需要Object
)。所以你的属性:
将被替换为:(
如果你将你的密钥指定为
string1
)。In Struts ActionMessage, you can specify value for your parameters
{0}
,{1}
,{2}
,{3}
specified in your properties file, as follows:Alternately:
value1
..value3
can be of any type (as Struts expects anObject
).so your property:
Will be replaced to:
(If you specify your key as
string1
).假设您有一个属性文件,它定义了消息的一些键,如下所示:
ActionMessage 类有许多构造函数,它们采用不同数量的参数。第一个是表示引用消息的键的字符串 - 在您的情况下,键是
string1
,它对应于消息{0} 是必需的。
;{0}
是某些动态内容的占位符。其余可能的参数是表示要替换这些占位符的实际值的对象。如果您执行
new ActionMessage("string1", "string2")
您将传入文字值string2
,最终将得到的输出
。
您需要做的是将“string2”替换为方法调用,该方法调用将获取与键“string2”对应的值。不过,这是我对这个问题的了解已经耗尽的地方,因此您需要自己对这部分进行一些研究。
Let's say you have a properties file that defines some keys for messages, like so:
The ActionMessage class has a number of constructors that take a varying number of arguments. The first is a string representing the key that refers to a message - in your case, the key is
string1
which corresponds to the message<li>{0} is required.</li>
; with the{0}
being a placeholder for some dynamic content.The remaining possible arguments are Objects that represent the actual values you want to replace those placeholders. If you do
new ActionMessage("string1", "string2")
you're passing in the literal valuestring2
, and you'll end up with output of<li>string2 is required.</li>
.What you need to do is replace
"string2"
with a method call that will get the value that corresponds to the keystring2
. This is where my knowledge of the problem runs out, though, so you'll need to do some research on this part for yourself.