4

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?

Mot
  • 28,248
  • 23
  • 84
  • 121
jeevs
  • 261
  • 6
  • 20
  • Post the actual code you're using. I doubt you're passing in the actual string `string2_in_properties_file` to the ActionMessage constructor - if you are, that's where you're going wrong. – Anthony Grist Jan 18 '12 at 14:42
  • I couldn't get you, can you elaborate a bit more?? – jeevs Jan 18 '12 at 15:13
  • Rather than doing `errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("string1_in_properties_file", "string2_in_properties_file"));` you want to be doing `errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("string1_in_properties_file", someMethodThatReturnsTheValueOfString2InPropertiesFile()));` – Anthony Grist Jan 18 '12 at 15:19
  • I've added an answer (even though it doesn't **fully** answer the question) explaining in more detail. The best answer I can give you is: I don't know. I would assume so, but this is where my knowledge runs out so I can't point you in the right direction. – Anthony Grist Jan 18 '12 at 15:45

4 Answers4

2

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

Didier Ghys
  • 30,396
  • 9
  • 75
  • 81
RickDavis
  • 2,276
  • 6
  • 25
  • 31
0

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.

kba
  • 19,333
  • 5
  • 62
  • 89
0

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

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • Yeah, i have read that in the ActionMessage Doc, But i want to use an application key at the place of value1. The 1st parameter to ActionMessage constructor is an application key similarly can't we give 2nd parameter an application key? – jeevs Jan 18 '12 at 15:11
  • No, you can't do that. ActionMessage only accept one key. Otherwise, use ResourceBundle & MessageFormat to format your resource string and call *new AcctionMessage(formattedResourceString, false).*. – Buhake Sindi Jan 18 '12 at 20:04
0

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.

Anthony Grist
  • 38,173
  • 8
  • 62
  • 76