Michael said:
Thanks for all your help so far. I ended up using a java Properties
object instead of going with Struts' MessageResources because I
couldn't get it to work. The properties object is working fine,
however, I still have the issue when using ActionErrors. I pass the
message key into the constructor, and am getting nothing displayed. I
am using Struts 1.1. I know that they changed the way the messages
are configured for the web app in 1.1 (moved it from the web.xml file
to the struts-config.xml). I am wondering if this is an issue. Any
help would be extremely helpful.
Just to re-itterate my issue, I am using Struts 1.1 in a WebSphere
cluster (2 nodes). Neither my code or Struts code can get the values
in my ApplicationResources.properties file. It workes fine in one
node, but when we deploy to two nodes, this problem appears.
Perhaps some code extracts would be of some assistance. Here's
the relevant section of my struts-config.xml:
<message-resources null="false" parameter="ApplicationResources"/>
This indicates that I MUST have the following file:
$APP_HOME/WEB-INF/classes/ApplicationResources.properties
Within a Struts Action I can access the messages resources
thusly:
MessageResources msgs = getResources( req );
(where req is the HttpServletRequest reference passed to the
execute method)
In order to invoke MessageResource#getMessage you'll need a
reference to the caller's locale. I posted this code a few
days ago:
Locale locale = getLocale( req );
Now you can pull the appropriate message and replace the
positional parameters by including the correct number of
arguments. Take a look at this message from the standard
Struts validator:
errors.minlength={0} can not be less than {1} characters.
I can generate the message string with the following code:
String msg = msgs.getMessage( locale, "errors.minlength",
"This field", "10" );
Of course, this is unnecessary when you're overriding the
validate method in a class which extends ActionForm. All
this "behind the scenes" work is done for you. Here's a
code snippet:
public ActionErrors validate( ActionMapping mapping,
HttpServletRequest req ) {
ActionErrors errors = new ActionErrors();
// add an error to the global errors
errors.add( ActionErrors.GLOBAL_ERROR,
new ActionError( "message_key" ) );
// add an error to a field name CCNUMBER
errors.add( "CCNUMBER",
new ActionError( "errors.minlength",
"Credit card number", "10" ) );
return( errors );
}
Does this help at all?