【问题标题】:Displaying struts validations message显示 struts 验证消息
【发布时间】:2012-01-18 14:29:04
【问题描述】:

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

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 = <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。

我知道可以通过对值进行硬编码来完成,但是还有其他方法吗?

【问题讨论】:

  • 发布您正在使用的实际代码。我怀疑你是否将实际字符串 string2_in_properties_file 传递给 ActionMessage 构造函数 - 如果你是,那就是你出错的地方。
  • 我听不懂,你能详细说明一下吗??
  • 与其做errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("string1_in_properties_file", "string2_in_properties_file"));,不如做errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("string1_in_properties_file", someMethodThatReturnsTheValueOfString2InPropertiesFile()));
  • 我已经添加了一个答案(即使它没有完全回答这个问题)更详细地解释了。我能给你的最好答案是:我不知道。我会假设是这样,但这是我的知识用完的地方,所以我无法为您指明正确的方向。

标签: java struts struts-1 struts-validation


【解决方案1】:

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

分别检索每个值
String sValue1 = getResources(request).getMessage(locale, "key1");
String sValue2 = getResources(request).getMessage(locale, "key2");

然后把它放到你的全局错误中

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

希望对您有所帮助....

【讨论】:

    【解决方案2】:

    很难确切地告诉你该怎么做,因为 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);
    

    上面会打印出来

    “猫”不是有效的输入。

    【讨论】:

      【解决方案3】:

      在 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)。

      【讨论】:

      • 是的,我在 ActionMessage Doc 中读过,但我想在 value1 的位置使用应用程序密钥。 ActionMessage 构造函数的第一个参数是应用程序密钥,同样我们不能给第二个参数一个应用程序密钥吗?
      • 不,你不能那样做。 ActionMessage 只接受一个键。否则,使用 ResourceBundle 和 MessageFormat 格式化您的资源字符串并调用 new AcctionMessage(formattedResourceString, false)..
      【解决方案4】:

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

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

      ActionMessage 类有许多构造函数,它们接受不同数量的参数。第一个是表示引用消息的键的字符串-在您的情况下,键是string1,对应于消息&lt;li&gt;{0} is required.&lt;/li&gt;{0} 是一些动态内容的占位符。

      其余可能的参数是表示您要替换这些占位符的实际值的对象。如果您执行new ActionMessage("string1", "string2"),您将传入文字值string2,您最终将得到&lt;li&gt;string2 is required.&lt;/li&gt; 的输出。

      您需要做的是将"string2" 替换为一个方法调用,该方法调用将获得与键string2 对应的值。不过,这就是我对这个问题的了解用完了,所以你需要自己对这部分做一些研究。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多