(Redirected from JavaBean)
In computing based on the Java Platform, JavaBeans are classes that encapsulate many objects into a single object (the bean). They are serializable, have a zero-argument constructor, and allow access to properties using getter and setter methods. The name "Bean" was given to encompass this standard, which aims to create reusable software components for Java.
It is a reusable software component written in Java that can be manipulated visually in an application builder tool.
The JavaBeans functionality is provided by a set of classes and interfaces in the java.beans package.
| Interface | Description |
|---|---|
| AppletInitializer | Methods in this interface are used to initialize Beans that are also applets. |
| BeanInfo | This interface allows the designer to specify information about the events, methods and properties of a Bean. |
| Customizer | This interface allows the designer to provide a graphical user interface through which a bean may be configured. |
| DesignMode | Methods in this interface determine if a bean is executing in design mode. |
| ExceptionListener | A method in this interface is invoked when an exception has occurred. |
| PropertyChangeListener | A method in this interface is invoked when a bound property is changed. |
| PropertyEditor | Objects that implement this interface allow the designer to change and display property values. |
| VetoableChangeListener | A method in this interface is invoked when a Constrained property is changed. |
| Visibility | Methods in this interface allow a bean to execute in environments where the GUI is not available. |
In order to function as a JavaBean class, an object class must obey certain conventions about method naming, construction, and behaviour. These conventions make it possible to have tools that can use, reuse, replace, and connect Java Beans.
The required conventions are as follows:
package player; public class PersonBean implements java.io.Serializable { /** Properties **/ private boolean deceased = false; private List list; /** Property "name", readable/writable. */ private String name = null; /** No-arg constructor (takes no arguments). */ public PersonBean() { } public List getList() { return list; } public void setList(final List list) { this.list = list; } /** * Getter for property "name". */ public String getName() { return name; } /** * Setter for property "name". * * @param value */ public void setName(final String value) { this.name = value; } /** * Getter for property "deceased" * Different syntax for a boolean field (is v.s. get) */ public boolean isDeceased() { return deceased; } /** * Setter for property "deceased". * @param value */ public void setDeceased(boolean value) { deceased = value; } }
TestPersonBean.java:
import player.PersonBean; /** * Class "TestPersonBean". */ public class TestPersonBean { /** * Tester method "main" for class "PersonBean". * * @param arguments */ public static void main(final String[] arguments) { final PersonBean person = new PersonBean(); person.setName("Bob"); person.setDeceased(false); person.setList(new ArrayList()); // Output: "Bob [alive]" System.out.print(person.getName()); System.out.println(person.isDeceased() ? " [deceased]" : " [alive]"); } }
<jsp:useBean id="person" class="player.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>