DEVFYI - Developer Resource - FYI

Do ActionForms have to be true JavaBeans?

Struts Questions and Answers


(Continued from previous question...)

56. Do ActionForms have to be true JavaBeans?

ActionForms are added to a servlet scope (session or request) as beans. What this means is that, for certain functionality to be available, your ActionForms will have to follow a few simple rules.
First, your ActionForm bean must have a zero-arguments constructor. This is required because Struts must be able to dynamically create new instances of your form bean class, while knowing only the class name. This is not an onerous restriction, however, because Struts will also populate your form bean's properties (from the request parameters) for you.
Second, the fields of your form bean are made available to the framework by supplying public getter and setter methods that follow the naming design patterns described in the JavaBeans Specification. For most users, that means using the following idiom for each of your form bean's properties:
private {type} fieldName;
public {type} getFieldName() {
return (this.fieldName);
}

public void setFieldName({type} fieldName) {
this.fieldName = fieldName;
}
NOTE - you MUST obey the capitalization conventions shown above for your ActionForm properties to be recognized. The property name in this example is "fieldName", and that must also be the name of the input field that corresponds to this property. A bean property may have a "getter" method and a "setter" method (in a form bean, it is typical to have both) whose name starts with "get" or "set", followed by the property name with the first character capitalized. (For boolean properties, it is also legal to use "is" instead of "get" as the prefix for the getter method.)
Advanced JavaBeans users will know that you can tell the system you want to use different names for the getter and setter methods, by using a java.beans.BeanInfo class associated with your form bean. Normally, however, it is much more convenient to follow the standard conventions.
WARNING - developers might be tempted to use one of the following techniques, but any of them will cause your property not to be recognized by the JavaBeans introspection facilities, and therefore cause your applications to misbehave:
* Using getter and setter method names that do not match - if you have a getFoo() method for your getter, but a setBar() method for your setter, Java will not recognize these methods as referring to the same property. Instead, the language will think you have a read-only property named "foo" and a write-only property named "bar".
* Using more than one setter method with the same name - The Java language lets you "overload" methods, as long as the argument types are different. For example, you could have a setStartDate(java.util.Date date) method and a setStartDate(String date) method in the same class, and the compiled code would know which method to call based on the parameter type being passed. However, doing this for form bean properties will prevent Java from recognizing that you have a "startDate" property at all.
There are other rules to follow if you want other features of your form beans to be exposed. These include indexed attributes and mapped attributes. They are covered in detail in other areas of the Struts documentation, in particular: indexedprops.html
For a complete explanation of what a JavaBean is, and everything it can do, see the JavaBeans Specification (version 1.01) at:
http://java.sun.com/products/javabeans/docs/beans.101.pdf

(Continued on next question...)

Other Interview Questions