原文地址:http://en.wikipedia.org/wiki/JavaBean#JavaBean_conventions
JavaBean conventions[edit]
In order to function as a JavaBean class, an object class must obey certainconventions about method naming, construction, and behaviour. These conventionsmake it possible to have tools that can use, reuse, replace, and connectJavaBeans.
The required conventions are as follows:
· The class must have a public defaultconstructor (with no-arguments). This allows easyinstantiation within editing and activation frameworks.
· The class properties must be accessible using get, set, is (used for boolean properties instead ofget), and other methods (so-called accessor methods and mutator methods) according to a standard namingconvention. This allows easy automated inspection and updating of bean state withinframeworks, many of which include custom editors for various types ofproperties. Setters can have one or more than one argument.
· The class should be serializable. [This allows applications and frameworksto reliably save, store, and restore the bean's state in a manner independentof the VM and of the platform.]
package player;
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 (isvs. 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(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:setPropertyname="person" property="*"/>
<html>
<body>
Name: <jsp:getProperty name="person" property="name"/><br/>
Deceased? <jsp:getProperty name="person" property="deceased"/><br/>
<br/>
<formname="beanTest"method="POST"action="testPersonBean.jsp">
Enter a name: <inputtype="text"name="name"size="50"><br/>
Choose an option:
<selectname="deceased">
<optionvalue="false">Alive</option>
<optionvalue="true">Dead</option>
</select>
<inputtype="submit"value="Test the Bean">
</form>
</body>
</html>
JavaBeanconventions[edit]
In order to function as a JavaBean class, an object class must obey certainconventions about method naming, construction, and behaviour. These conventionsmake it possible to have tools that can use, reuse, replace, and connectJavaBeans.
The required conventions are as follows:
· The class must have a public defaultconstructor (with no-arguments). This allows easyinstantiation within editing and activation frameworks.
· The class properties must be accessible using get, set, is (used for boolean properties instead of get),and other methods (so-called accessor methods and mutator methods) according to a standard namingconvention. This allows easy automated inspection and updating of bean state withinframeworks, many of which include custom editors for various types ofproperties. Setters can have one or more than one argument.
· The class should be serializable. [This allows applications and frameworksto reliably save, store, and restore the bean's state in a manner independentof the VM and of the platform.]
package player;
public class PersonBean implements java.io.Serializable {
private String name = null;
private boolean deceased = false;
/** No-argconstructor (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(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 ofPersonBean in a JSP. %>
<jsp:useBean id="person"class="beans.PersonBean"scope="page"/>
<jsp:setProperty name="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">
Enter a name: <inputtype="text"name="name"size="50"><br/>
Choose an option:
<selectname="deceased">
<optionvalue="false">Alive</option>
<optionvalue="true">Dead</option>
</select>
<inputtype="submit"value="Test theBean">
</form>
</body>
</html>