我想显示在 Action 类中检测到的错误,我使用:
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"));
其中字符串 1 = <li>{0} is required.</li>
.
然后显示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。
我知道可以通过对值进行硬编码来完成,但是还有其他方法吗?
最佳答案
因为你想从属性文件中获取两个键的值,并将其放入全局错误键中, 我会说,使用
分别检索每个值String sValue1 = getResources(request).getMessage(locale, "key1");
String sValue2 = getResources(request).getMessage(locale, "key2");
然后把它放在你的全局错误中
errors.add(ActionErrors.GLOBAL_MESSAGE,sValue1+"<br/>"+sValue2);
希望对你有帮助....
关于java - 显示 struts 验证消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8911807/