From:
JavaBeans are for that can be manipulated visually in a builder tool. Practically, they are classes written in the
conforming to a particular convention. They are used to encapsulate
many objects into a single object (the bean), so that they can be passed
around as a single bean object instead of as multiple individual
objects. A JavaBean is a Java Object that is , has a , and allows access to properties using .
JavaBean conventions
In order to function as a JavaBean ,
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 JavaBeans.
The required conventions are as follows:
- The class must have a public (no-argument). This allows easy instantiation within editing and activation frameworks.
- The class must be accessible using get, set, is (used for boolean properties instead of get) and other methods (so-called and ), following a standard .
This allows easy automated inspection and updating of bean state within
frameworks, many of which include custom editors for various types of
properties.
- The class should be . It allows applications and frameworks to reliably save, store, and restore the bean's state in a fashion independent of the and of the platform.
JavaBean Example
PersonBean.java:
- package beans;
-
-
/**
-
* Class PersonBean.
-
*/
-
public class PersonBean implements java.io.Serializable {
-
-
private String name;
-
-
private boolean deceased;
-
-
/** No-arg constructor (takes no arguments). */
-
public PersonBean() {
-
}
-
-
/**
-
* Property name (note capitalization) readable/writable.
-
*/
-
public String getName() {
-
return this.name;
-
}
-
-
/**
-
* Setter for property name.
-
* @param name
-
*/
-
public void setName(final String name) {
-
this.name = name;
-
}
-
-
/**
-
* Getter for property "deceased"
-
* Different syntax for a boolean field (is vs. get)
-
*/
-
public boolean isDeceased() {
-
return this.deceased;
-
}
-
-
/**
-
* Setter for property deceased.
-
* @param deceased
-
*/
-
public void setDeceased(final boolean deceased) {
-
this.deceased = deceased;
-
}
-
}
TestPersonBean.java:
- import beans.PersonBean;
-
-
/**
-
* Class TestPersonBean.
-
*/
-
public class TestPersonBean {
-
/**
-
* Tester method main for class PersonBean.
-
* @param args
-
*/
-
public static void main(String[] args) {
-
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>
阅读(1064) | 评论(0) | 转发(0) |