Chitika

Friday, September 10, 2004

limitations of ActionForm in Struts

Up until now, Struts have been used quite extensively throughout the world of J2EE Web developers. I myself just try to start using them a little bit. One of the few things I've encountered is the limitations of ActionForm. One limitation I'd like to cover here is the limitation of the automatic population strategy implemented by Struts.

Struts currently allows only String and boolean to be automatically converted into the ActionForm properties. It leaves the rest of the data types for the developers to implement them. As the book Struts in Action suggested, creating helper methods might solve the problem, for example:

private double approvedMinimum;

public String getApprovedMinimumText () {
  return StringConversionUtil.doubleToString (
    approvedMinimum,
    2);
}

Assuming we had the StringConversionUtil class with this method:

public static String doubleToString (double d, int digit) {
  StringBuffer pattern = new StringBuffer ("#");
  if (digit > 0) {
    pattern.append (".");
    for (int i=0; i<=0; i++) {
      pattern.append ("0");
    }
  }
  NumberFormat nf = new DecimalFormat (pattern.toString());
  return nf.format (d);
}

This method will be used to populate the approvedMinimum, which was supposedly to be a double value. A similar method must also be created to parse the user-inputted String into a double on the way in. The same applies for other non-String, non-boolean data type, such as Calendar, etc.

Another option is to declare all properties in the ActionForm as String, as suggested by the Programming Jakarta Struts book. It claims that the ActionForm was not designed as the [m]odel holding the real data, instead it's merely a representation of the [v]iew. Hence, this approach might force us to implement the conversion manually before setting the value into the ActionForm.

I'd rather go with the first approach..

Are these the only options to go?
Are there any other options which just might be better than those books have to offer?
I sure would love to hear... :D


No comments:

Post a Comment