Struts ActionForms and accessor/mutator "symmetry"


Web tier: servlets, JSP, Web frameworks: Struts ActionForms and accessor/mutator "symmetry"

  1. Is it generally considered a bad idea for ActionForm properties to have types other than String? In particular, I am considering the following architecture:

    • ActionForm mutators will have signatures such as void setProperty(String prop)
    • ActionForm accessors for non-string properties will have signatures such as int getProperty()
    • validate() will be responsible for doing the checks to ensure that accessors will succeed in converting the String to the necessary type for the property

    I see several advantages in this approach. First of all, it allows me to handle invalid types gracefully, by providing the user with an appropriate ActionMessage, rather than just throwing an exception when the conversion is attempted. Second, it provides my Action with an ActionForm that has already "converted" its properties to the appropriate types, and thus the Action is not responsible for doing so.

    However, something about the disymmetry (is that a word?) of this arrangement bothers me. Can anyone confirm or deny that this is a good approach, and if not, why?

  2. A little experimentation, and I can answer my own question...

    When implemented as follows, this setup will not work:

    public void SomeForm extends ActionForm {
       private String testProperty;

       public void setTestProperty(String s) {
          this.testProperty = s;

       public int getTestProperty() {
          return Integer.parseInt(this.s);

       public ... validate(...) {
          try {
          } catch (NumberFormatException e) {
             //add Action Message

    Struts blows up when the page loads and getTestProperty() is called on a new form. There are a number of ways I could force it to work (use separate instance variables for the unconverted and converted types, initialize the property to something valid, etc...), but these make things more complicated rather than less...

    I'll just use String for everything and check validity in validate, and convert as necessary in my Action.