Sunday, November 18, 2012

A Java Bean is just a standard


A Java Bean is just a standard
  1. All properties private (use getters/setters)
  2. A public no-argument constructor
  3. Implements Serializable.
That's it. It's just a convention. Lots of libraries depend on it though....
With respect to Serializable, from the API:
Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.
In other words, serializable objects can be written to streams, and hence files, object databases, anything really.
Also, there is no syntactic difference between a Java Bean and another class -- a class defines a Java Bean if it follows the standards.
There is a term for it because the standard allows libraries to programmatically do things with class instances you define in a predefined way. For example, if a library wants stream any object you pass into it, it knows it can because your object is serializable (assuming the lib requires your objects be proper Java Beans).


PersonBean.java:
package beans;
 
/**
 * Class <code>PersonBean</code>.
 */
public class PersonBean implements java.io.Serializable {
 
    private String name = null;
 
    private boolean deceased = false;
 
    /** No-arg constructor (takes no arguments). */
    public PersonBean() {
    }
 
    /**
     * Property <code>name</code> (note capitalization) readable/writable.
     */
    public String getName() {
        return name;
    }
 
    /**
     * Setter for property <code>name</code>.
     * @param NAME
     */
    public void setName(final String NAME) {
        name = NAME;
    }
 
    /**
     * Getter for property "deceased"
     * Different syntax for a boolean field (is vs. get)
     */
    public boolean isDeceased() {
        return deceased;
    }
 
    /**
     * Setter for property <code>deceased</code>.
     * @param DECEASED
     */
    public void setDeceased(final boolean DECEASED) {
        deceased = DECEASED;
    }
}
TestPersonBean.java:
import beans.PersonBean;
 
/**
 * Class <code>TestPersonBean</code>.
 */
public class TestPersonBean {
    /**
     * Tester method <code>main</code> for class <code>PersonBean</code>.
     * @param ARGS
     */
    public static void main(final String[] ARGS) {
        final PersonBean PERSON = new PersonBean();
 
        PERSON.setName("Bob");
        PERSON.setDeceased(false);
 
        // Output: "Bob [alive]"
        System.out.print(PERSON.getName());
        System.out.println(PERSON.isDeceased() ? " [deceased]" : " [alive]");
    }
}
testPersonBean.jsp;
<% // Use of PersonBean in a JSP. %>
<jsp:useBean id="person" class="beans.PersonBean" scope="page"/>
<jsp:setProperty name="person" property="*"/>
 
<html>
    <body>
        Name: <jsp:getProperty name="person" property="name"/><br/>
        Deceased? <jsp:getProperty name="person" property="deceased"/><br/>
        <br/>
        <form name="beanTest" method="POST" action="testPersonBean.jsp">
            Enter a name: <input type="text" name="name" size="50"><br/>
            Choose an option:
            <select name="deceased">
                <option value="false">Alive</option>
                <option value="true">Dead</option>
            </select>
            <input type="submit" value="Test the Bean">
        </form>
    </body>
</html>

No comments:

Post a Comment