显示 struts 验证消息

发布于 2024-12-28 01:23:32 字数 763 浏览 6 评论 0原文

我想显示在操作类中检测到的错误,我使用:

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 =

  • {0} is required.
  • 然后它显示 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 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(4

    佼人 2025-01-04 01:23:32

    由于您想从属性文件中获取两个键的值,并将其放入全局错误键中,
    我想说,使用单独检索每个值

    String sValue1 = getResources(request).getMessage(locale, "key1");
    String sValue2 = getResources(request).getMessage(locale, "key2");
    

    ,然后将其放入全局错误中

    errors.add(ActionErrors.GLOBAL_MESSAGE,sValue1+"<br/>"+sValue2);
    

    希望它有帮助......

    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

    String sValue1 = getResources(request).getMessage(locale, "key1");
    String sValue2 = getResources(request).getMessage(locale, "key2");
    

    and then put it in your global error

    errors.add(ActionErrors.GLOBAL_MESSAGE,sValue1+"<br/>"+sValue2);
    

    Hope it help....

    无需解释 2025-01-04 01:23:32

    很难准确地告诉您要做什么,因为 O 不知道 errorsActionMessage 背后的代码。但是您可以使用String.format。您的代码看起来像这样

    public class ActionErrors {
        public static final String INVALID_INPUT "'%s' is not valid input.";
        ...
    }
    

    String input = "Cats";
    String message = String.format(ActionErrors.INVALID_INPUT, input);
    System.out.println(message);
    

    上面将打印

    “猫”不是有效的输入。

    It's hard to tell you exactly what to do, since O don't know the code behind errors and ActionMessage. But you can, however, use String.format. Your code would look something like this

    public class ActionErrors {
        public static final String INVALID_INPUT "'%s' is not valid input.";
        ...
    }
    

    and

    String input = "Cats";
    String message = String.format(ActionErrors.INVALID_INPUT, input);
    System.out.println(message);
    

    The above will print

    'Cats' is not valid input.

    治碍 2025-01-04 01:23:32

    在Struts中 ActionMessage ,您可以指定参数值 {0}{1}{2}{3} > 在属性文件中指定,如下所示:

    errors.add(ActionErrors.GLOBAL_MESSAGE,
      new ActionMessage("some_string_in_properties_file", "value1"));
    

    或者:

    errors.add(ActionErrors.GLOBAL_MESSAGE,
      new ActionMessage("some_string_in_properties_file", "value1", "value2", "value3"));
    

    value1..value3 可以是任何类型(因为 Struts 需要 Object)。

    所以你的属性:

    string1 = <li>{0} is required.</li>
    

    将被替换为:(

    <li>value1 is required.</li>
    

    如果你将你的密钥指定为string1)。

    In Struts ActionMessage, you can specify value for your parameters {0}, {1}, {2}, {3} specified in your properties file, as follows:

    errors.add(ActionErrors.GLOBAL_MESSAGE,
      new ActionMessage("some_string_in_properties_file", "value1"));
    

    Alternately:

    errors.add(ActionErrors.GLOBAL_MESSAGE,
      new ActionMessage("some_string_in_properties_file", "value1", "value2", "value3"));
    

    value1..value3 can be of any type (as Struts expects an Object).

    so your property:

    string1 = <li>{0} is required.</li>
    

    Will be replaced to:

    <li>value1 is required.</li>
    

    (If you specify your key as string1).

    苍白女子 2025-01-04 01:23:32

    假设您有一个属性文件,它定义了消息的一些键,如下所示:

    string1: <li>{0} is required.</li>
    string2: Username
    

    ActionMessage 类有许多构造函数,它们采用不同数量的参数。第一个是表示引用消息的键的字符串 - 在您的情况下,键是 string1 ,它对应于消息

  • {0} 是必需的。; {0} 是某些动态内容的占位符。
  • 其余可能的参数是表示要替换这些占位符的实际值的对象。如果您执行 new ActionMessage("string1", "string2") 您将传入文字值 string2,最终将得到 的输出

  • string2 是必需的。
  • 您需要做的是将“string2”替换为方法调用,该方法调用将获取与键“string2”对应的值。不过,这是我对这个问题的了解已经耗尽的地方,因此您需要自己对这部分进行一些研究。

    Let's say you have a properties file that defines some keys for messages, like so:

    string1: <li>{0} is required.</li>
    string2: Username
    

    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 value string2, 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 key string2. This is where my knowledge of the problem runs out, though, so you'll need to do some research on this part for yourself.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文