JavaBeans
In computing based on the Java Platform, JavaBeans is a technology developed by Sun Microsystems and released in 1996, as part of JDK 1.1. The 'beans' of JavaBeans are classes that encapsulate one or more objects into a single standardized object (the bean). This standardization allows the beans to be handled in a more generic fashion, allowing easier code reuse and introspection. This in turn allows the beans to be treated as software components, and to be manipulated visually by editors and IDEs without needing any initial configuration, or to know any internal implementation details. As part of the standardization, all beans must be serializable, have a zero-argument constructor, and allow access to properties using getter and setter methods. Features
Advantages
Disadvantages
JavaBeans APIThe JavaBeans functionality is provided by a set of classes and interfaces in the
JavaBean conventionsIn 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:
Code examplepackage 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 vs get)
*/
public boolean isDeceased() {
return deceased;
}
/**
* Setter for property "deceased".
* @param value
*/
public void setDeceased(boolean value) {
deceased = value;
}
}
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>
See alsoReferences
External links |
Portal di Ensiklopedia Dunia