JavaWeb:What is JavaBean?

Talk is cheap

摘自《Murach’s Java Servlets and JSP》
机翻来自有道

First, a JavaBean must contain a zero-argument constructor, which is a constructor that doesn’t accept any arguments. In this figure, the zero-argument constructor uses three statements to set all three instance variables equal to empty strings. As a result, a newly created User object stores empty strings for its instance variables instead of null values, which is usually what you want.

首先,JavaBean必须包含一个零参数构造函数,这个构造函数不接受任何参数。在该图中,零参数构造函数使用三条语句将所有三个实例变量设置为空字符串。因此,新创建的用户对象为其实例变量存储空字符串,而不是空值,这通常是您想要的。

Second, a JavaBean must contain get and set methods for all of the properties that need to be accessed by JSPs. In this figure, for example, the methods provide access to all of the instance variables of the User class, so this class qualifies as a bean. Of course, you can also code get and set methods that provide access to other properties in a bean.

其次, JavaBean 必须 包含 get 和 set 方法 的 所有 properties 需要 由 JSPs.
访问例如,在这个图中,方法提供了对User类的所有实例变量的访问,因此这个类符合bean的条件。当然,您也可以编写get和set方法来提供对bean中其他属性的访问。

To provide access to a Boolean value, you code is and set methods instead of get and set methods. For example, you could code methods named isEmailUpdated and setEmailUpdated to provide access to a Boolean property named emailUpdated.

要提供对布尔值的访问,需要编写is和set方法,而不是get和set方法。例如,您可以编写名为isEmailUpdated和setEmailUpdated的方法来提供对布尔属性emailUpdated的访问

When you code the get, set, and is methods, you must follow the capitalization conventions used in this figure. In other words, each method name must start with a lowercase letter, followed by a property name that starts with an uppercase letter as in firstName.

当你使用 get, set, 和 is方法时, 必须 遵循 capitalization 惯例 用于 figure,换句话说, 每个方法名称必须以小写开始 , 后跟一个开始于大写字母的属性名,一个示例是 firstName

Third, a JavaBean must implement the Serializable or Externalizable interface. The Serializable interface is a tagging interface in the java.io package that indicates that a class contains get, set, and is methods that another class can use to read and write an object’s instance variables to and from a persistent data source. In this figure, for example, the User class implements the Serializable interface and contains all the necessary get and set methods. As a result, some
servlet engines can save and restore this object if that’s necessary. For example,
the Tomcat container can save the User object’s state before it shuts down, and it
can restore the User object’s state when it starts up the next time.

Third, JavaBean 必须 实现 Serializable 或 Externalizable
interface.Serializable接口是java中的一个标记接口。io包,指示类包含get、set和is方法,另一个类可以使用这些方法向持久数据源读写对象的实例变量。例如,在这个图中,User类实现了Serializable接口,并包含了所有必需的get和set方法。结果,一些
如果需要,servlet引擎可以保存和恢复这个对象。例如,
Tomcat容器可以在用户对象关闭之前保存它的状态
可以在下次启动时恢复用户对象的状态。

When coding a web application, it’s common to use JavaBeans to define the
business objects of an application. These beans can be called invisible JavaBeans
because they don’t define visible components. The focus of this book is on this
type of JavaBean.
You should realize, though, that JavaBeans are capable of doing much more
than defining business objects. For instance, JavaBeans can be used to define
buttons and other user interface controls.

在编写web应用程序时,通常使用javabean来定义

应用程序的业务对象。这些bean可以称为不可见的javabean

因为它们不定义可见组件。这本书的重点就是这个类型的JavaBean。

但是,您应该认识到javabean可以做更多的事情

而不是定义业务对象。例如,可以使用javabean来定义

按钮和其他用户界面控件。

You should also realize that there’s another type of JavaBean called an
Enterprise JavaBean (EJB). Although EJBs are similar in some ways to
JavaBeans, EJBs are more complex and difficult to code than JavaBeans. To
learn more about them, you can get a book that covers the advanced features of
the Java EE specification.

您还应该认识到,还有另一种类型的JavaBean称为an

企业JavaBean (EJB)。尽管ejb在某些方面与

与javabean相比,ejb更复杂、更难编码。来

了解更多关于他们,你可以得到一本书,涵盖了先进的功能

Java EE规范。

Show me the code

package murach.business;
import java.io.Serializable;

public class User implements Serializable{
	private String firstName;
	private String lastName;
	private String email;
	
	public User() {
		firstName="";
		lastName="";
		email="";
	}
	public User(String firstName,String lastName,String email) {
		this.firstName=firstName;
		this.lastName=lastName;
		this.email=email;
	}
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
}

以这段代码为例解释一下
JavaBean是一个特殊的Java类,遵守JavaBean API规范

第一
它必须implements Serializable或者Externalizable两接口其中之一
Serializable和Externalizable

第二
它必须有无参构造方法
如果你重写了自己的构造方法,一定要再写一个无参构造方法

第三
它必须对外提供一系列的get和set方法使外界可对其私有变量作相应操作
根据属性不同可分为:只读(只有get方法)、只写(只有set方法)、读写(都有)

Eclipse自动为变量生成Get和Set方法

声明变量后

右击空白 --> Source --> Generate Getters and Setters
–> 选择要生成的方法 -->Generate

在这里插入图片描述
会自动生成符合命名规范的方法
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值