Value replacement in Message Resource Bundle


When you constructed the web application, earlier in this chapter, you used static messages in the Resource Bundle. However consider this: You have a dozen fields in the form. The only validation rule is that all fields are required. Hence the error messages for each field differs from another only by the field name . First name is required , Last name is required , Age is required and so on. It would be ideal if there were a field name replacement mechanism into a fixed error message template. The good news is that it already exists. In the resource bundle file, you can define a template for the above error message as:

 errors.required={0} is required. 

In the validate() method, the ActionError is then constructed using one of the following overloaded constructors.

 public ActionError(String key, Object value0);     public ActionError(String key, Object value0, Object value1)     . . .     public ActionError(String key, Object[] values); 

The first overloaded constructor accepts the key and one replacement value. The second overloaded constructor accepts a key and two replacement values. The last constructor accepts a key and an array of objects for replacement. You can now construct an ActionError for the first name as follows :

 String[] strArray = {First name};    ActionError err = new ActionError(errors.required strArray); 

This will result in an error message: First name is required. Beautiful isn ‚ t it! Now you can make this even better. Notice that in the above example, we hard coded the field name in the replacement value array in the process of reducing the set of error messages to a single error message template. Now, let us go one step further and get the field name from the resource bundle too. The following code shows how to do it.

 MessageResources msgRes =     (MessageResources) request.getAttribute(Globals.MESSAGES_KEY); String firstName =       msgRes.getMessage(prompt.customer.firstname); ActionError err = new ActionError(errors.required firstName); 
  • First, a MessageResources for the current module is obtained.

  • Next, the display value of the first name field is obtained from the MessageResources (resource bundle) in the getMessage() method by using the key for the first name ‚ prompt.customer.firstName .

  • Finally, the display value of the first name field is used as a replacement parameter in the ActionError using the first of the overloaded constructors.

This is generally the preferred way of constructing reusable error messages when the validate() method is coded manually.

Tip ‚  

Using the struts-blank.war as a template

In this application we put together everything from scratch to construct the application. You can use the template so constructed for future use or you can use the ready-made template available in the Struts distribution. The ready-made template is called struts-blank.war is something that you can unwar and use as template for your applications. It has all the tlds and jars included in the WAR. Plus it provides the web.xml and struts-config.xml ready to be used as placeholders with default values.




Struts Survival Guide. Basics to Best Practices
Struts Survival Guide: Basics to Best Practices (J2ee Survival Series)
ISBN: 0974848808
EAN: 2147483647
Year: 2004
Pages: 96

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net