首页 > 代码库 > 什么是JavaBeans?
什么是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实现序列化
package player;public class PersonBean implements java.io.Serializable { /** * Property <code>name</code> (note capitalization) readable/writable. */ private String name = null; private boolean deceased = false; private List list; public List getList() { return list; } public void setList(List list) { this.list= list; } /** No-arg constructor (takes no arguments). */ public PersonBean() { } /** * Getter for property <code>name</code> */ public String getName() { return name; } /** * Setter for property <code>name</code>. * @param value */ public void setName(final String value) { name = value; } /** * 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 value */ public void setDeceased(final boolean value) { deceased = value; }}
在JSP页面中使用PersonBean:
<% // Use of PersonBean in a JSP. %><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="http://www.mamicode.com/false">Alive</option> <option value="http://www.mamicode.com/true">Dead</option> </select> <input type="submit" value="http://www.mamicode.com/Test the Bean"> </form> </body></html>
什么是JavaBeans?
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。