java beans_什么是JavaBeans?

参看维基百科,归纳出以下几条:

JavaBeans是指符合某些标准的类,

Bean这个名称用于涵盖这个标准,

其目的在于创建可重用的Java组件。

由于Bean是很“死板”的东西,因此它可以持久存储,并可以借助辅助软件快速实现。

Bean有它专属的一套API。

JavaBean conventions[edit]

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:

The class must have a public default constructor (with no arguments). This allows easy instantiation within editing and activation frameworks.

The class properties must be accessible using get, set, is (can be used for boolean properties instead of get), and other methods (so-called accessor methods and mutator methods) according to a standard naming convention. This allows easy automated inspection and updating of bean state within frameworks, many of which include custom editors for various types of properties. Setters can have one or more than one argument.

The class should be serializable. (This allows applications and frameworks to reliably save, store, and restore the bean's state in a manner independent of the VM and of the platform.)

实例,1无参构造器 2getter, setter 3实现序列化

packageplayer;public class PersonBean implementsjava.io.Serializable {/*** Property name (note capitalization) readable/writable.*/

private String name = null;private boolean deceased = false;privateList list;publicList getList() {returnlist;

}public voidsetList(List list) {this.list=list;

}/**No-arg constructor (takes no arguments).*/

publicPersonBean() {

}/*** Getter for property name*/

publicString getName() {returnname;

}/*** Setter for property name.

*@paramvalue*/

public void setName(finalString value) {

name=value;

}/*** Getter for property "deceased"

* Different syntax for a boolean field (is vs. get)*/

public booleanisDeceased() {returndeceased;

}/*** Setter for property deceased.

*@paramvalue*/

public void setDeceased(final booleanvalue) {

deceased=value;

}

}

在JSP页面中使用PersonBean:


Deceased? Enter a name:
Choose an option:

Alive

Dead

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值