This article has multiple issues. Please helpimprove it or discuss these issues on thetalk page.(Learn how and when to remove these messages) (Learn how and when to remove this message)
|
In computing based on theJava Platform,JavaBeans is a technology developed bySun Microsystems and released in 1996, as part ofJDK 1.1.
The 'beans' of JavaBeans are classes that encapsulate one or moreobjects into a single standardized object (the bean). This standardization allows the beans to be handled in a more generic fashion, allowing easiercode reuse andintrospection. This in turn allows the beans to be treated assoftware components, and to be manipulated visually byeditors and IDEs without needing any initial configuration, or to know any internal implementation details.
As part of the standardization, all beans must beserializable, have azero-argument constructor, and allow access to properties usinggetter and setter methods.
The JavaBeans functionality is provided by a set of classes and interfaces in thejava.beans package.
| Interface | Description |
|---|---|
| AppletInitializer | Methods in this interface are used to initialize Beans that are alsoapplets. |
| 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 JavaBeanclass, 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:
packageorg.wikipedia.players;importjava.io.Serializable;importjava.util.List;publicclassPersonBeanimplementsSerializable{/** Properties **/privatebooleandeceased=false;privateList<String>list;/** Property "name", readable/writable. */privateStringname=null;/** No-arg constructor (takes no arguments). */publicPersonBean(){}publicList<String>getList(){returnlist;}publicvoidsetList(finalList<String>list){this.list=list;}/** * Getter for property "name". */publicStringgetName(){returnname;}/** * Setter for property "name". * * @param value */publicvoidsetName(finalStringvalue){this.name=value;}/** * Getter for property "deceased" * Different syntax for a boolean field (is vs get) */publicbooleanisDeceased(){returndeceased;}/** * Setter for property "deceased". * @param value */publicvoidsetDeceased(booleanvalue){deceased=value;}}
TestPersonBean.java:
packageorg.wikipedia.players;importjava.util.ArrayList;importorg.wikipedia.players.PersonBean;/** * Class "TestPersonBean". */publicclassTestPersonBean{/** * Tester method "main" for class "PersonBean". * * @param arguments */publicstaticvoidmain(String[]args){finalPersonBeanperson=newPersonBean();person.setName("Bob");person.setDeceased(false);person.setList(newArrayList<String>());// Output: "Bob is [alive]"System.out.printf("%s is %s%n",person.getName(),person.isDeceased()?" [deceased]":" [alive]");}}
<jsp:useBeanid="person"class="org.wikipedia.players.PersonBean"scope="page"/><jsp:setPropertyname="person"property="*"/><html><body>Name:<jsp:getPropertyname="person"property="name"/><br/>Deceased?<jsp:getPropertyname="person"property="deceased"/><br/><br/><formname="beanTest"method="POST"action="testPersonBean.jsp">Enteraname:<inputtype="text"name="name"size="50"><br/>Chooseanoption:<selectname="deceased"><optionvalue="false">Alive</option><optionvalue="true">Dead</option></select><inputtype="submit"value="Test the Bean"></form></body></html>