Core JavaBean Specifications
To be recognized as a standard JavaBean, a class must adhere to a specific set of design conventions. These rules ensure compatibility with various frameworks and tools that rely on reflection.
- Constructor: The class must provide a public no-argument constructor.
- Accessors: Properties are exposed via public getter and setter methods. If only a getter is provided, the property is considered read-only.
- Property Definition: Properties are defined by the accessor methods, not necessarily by member fields. A property can exist solely based on the presence of valid get/set methods.
- Naming Conventions: Method names must follow standard patterns. For boolean types, the getter method may start with either
getoris.
Introspection and Utility Libraries
Java introspection allows programs to analyze the structure of a bean at runtime. The process typically involves generating a BeanInfo object, which contains PropertyDescriptor instances. These descriptors map property names to their corresponding getter and setter methods, enabling reflective access.
The Apache Commons BeanUtils library leverages this introspection mechanism to simplify property manipulation. To utilize this library, ensure the following dependency are included in the project classpath:
commons-beanutils.jarcommons-logging.jar
Key operations provided by the BeanUtils class include:
// Retrieve a property value as a String
String value = BeanUtils.getProperty(targetObject, "propertyName");
// Assign a String value to a property
BeanUtils.setProperty(targetObject, "propertyName", "newValue");
// Populate a bean from a Map of values
BeanUtils.populate(targetObject, valueMap);
// Convert a Map directly into a Bean instance
MyBean instance = BeanUtils.toBean(valueMap, MyBean.class);
When importing, insure wildcard imports are used correctly (e.g., import org.apache.commons.beanutils.*;).
Nested Property Navigation
JavaBeans often contain references to other beans, creating a nested structure. Frameworks can navigate these chains using dot notation. The following example demonstrates setting up a user object with a embedded profile object:
Profile location = new Profile();
location.setRegion("Shanghai");
location.setDistrict("Pudong");
User member = new User();
member.setAccountId("U1001");
member.setCredits(5000);
member.setProfile(location);
request.setAttribute("currentUser", member);
JSP Standard Actions for Beans
JavaServer Pages provide standard XML tags to interact with JavaBeans without writing scriptlets. These actions handle instantiation, property assignment, and value retrieval.
- <jsp:useBean>: Instantiates a bean or locates an existing one within a specified scope. ```
<jsp:useBean id="sessionUser" class="com.example.model.Account" scope="session" />
This tag searches the session scope for an attribute named `sessionUser`. If not found, it instantiates `com.example.model.Account`. - <jsp:setProperty>: Assigns values to bean properties. ```
<jsp:setProperty name="sessionUser" property="accountName" value="administrator" />
- <jsp:getProperty>: Retrieves and outputs a property value. ```
<jsp:getProperty name="sessionUser" property="accountName" />