Using JavaBeans with Struts Tags

In many cases, you’ll use the tags from the various tag libraries in conjunction with JavaBeans. The JavaBeans may be ActionForms, whose properties correspond to input fields in the HTML form. In other cases, however, the beans will be ordinary value objects from the model layer. These beans can be in any scope: page, request, session, or application.

There are three ways to access the properties of a JavaBean:

  • Accessing simple properties

  • Accessing nested properties

  • Accessing indexed properties

Accessing Simple Properties

Accessing simple bean properties works similarly to the JSP <jsp:getProperty> action. A reference to a property named “firstName” is converted into a method call to getFirstName() or setFirstName( value ), using the standard JavaBeans specification naming conventions for bean properties.

Struts uses the Java introspection APIs to identify the names of the actual property getter and setter methods, so your beans can provide customized method names through the use of a BeanInfo class. See the JavaBeans specification, available at http://java.sun.com/products/javabeans, for more information.

Accessing Nested Properties

Nested references are used to access a property through a hierarchy of property names separated by periods (.), similar to the way that nested properties are accessed in JavaScript. For example, the following property reference:

property="user.address.city"

is translated into the equivalent Java expression:

getUser( ).getAddress( ...

Get Programming Jakarta Struts, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.