Processing Localized Input


Localized input is data input in a native language using locale specific formats. How does your back-end Java code process data input in a native language? Let us consider a simple form with two fields, fullName and monthlySalary as shown below.

 public class CustomerForm extends ActionForm {     private String fullName = null;     private double monthlySalary = 0.0; ... } 

John Doe enters his monthly salary as 5431.52 and submits the form. That ‚ s it, the form fields are populated nicely and the application works without a hitch. The conversion of the monthly salary from a String to a double is automatically taken care of by Struts and John Doe won ‚ t have any problems with the application.

What happens if the same application is viewed by a user in France and he decides to enter the same amount in the French format as 5 431,52? When the French user submits the application, the monthlySalary attribute in CustomerForm ends up being populated with 0.0 instead of 5431.52. Why so? When the form is submitted, the RequestProcessor populates the JavaBeans properties of the ActionForm with the request parameters by using the RequestUtils and BeanUtils classes. The actual population is done by the BeanUtils.populate() method. That method tries to parse the String ‚“5 431,52 ‚½ and assign it to monthlySalary ‚ a double field without caring much for the Locale of the user. This obviously throws an exception on which the default action is to set 0.0 in the monthlySalary field.

What is the solution then? How can you make the Struts applications process localized input? Since the BeanUtils class does not check the locale at the time of populating the form, the only way out of this situation is to make the monthlySalary field a String instead of a double . Now, the BeanUtils does not try to parse a double from the String. Instead the value is assigned AS IS. A customized routine has to be written to convert the String into a double in a Locale dependent manner.




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