ActionForms and Value Objects - Repetition of Code?

Ted Husted

Struts doesn't really need an ActionForm for every form -- it needs a form-bean for every form. In most cases, you can simply define one master ActionForm for your application, with all the properties it uses, and then register it under different form-bean names. This will give each form its own attribute name in the servlet contexts, so that you can have more than one form on a page without anything colliding. The Struts-Validator also goes by the attribute name, so that you can have a different formset for each form-bean, even if they all share the same ActionForm type. Worst case, you can use a base "property bean" ActionForm and subclass it with different validate methods, if needed.

While ActionForms may resemble value objects, they fill a very different purpose. They are really buffers for the HTML controls, and give you a chance to validate untrusted input before passing it along to value objects. HTTP is a wild protoocol and exposing objects designed for another purpose to a Web browser may have unforseen results. Also, HTTP is string-based, everything comes in as a string and somebody has to cope with that.

See also

In Struts 1.1 beta, a Map can be used instead of individual properties on an ActionForm. In the future, I foresee Struts developers using the same base ActionForm in all their projects, without going through the hassle of defining all these String properites.

HTH, Ted