سؤال

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?

هل كانت مفيدة؟

المحلول

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....

نصائح أخرى

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.

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).

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top