Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2562009
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2011-07-20 11:03:54

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:

  1. package beans;
  2.  
  3. /**
  4.  * Class PersonBean.
  5.  */
  6. public class PersonBean implements java.io.Serializable {
  7.  
  8.     private String name;
  9.  
  10.     private boolean deceased;
  11.  
  12.     /** No-arg constructor (takes no arguments). */
  13.     public PersonBean() {
  14.     }
  15.  
  16.     /**
  17.      * Property name (note capitalization) readable/writable.
  18.      */
  19.     public String getName() {
  20.         return this.name;
  21.     }
  22.  
  23.     /**
  24.      * Setter for property name.
  25.      * @param name
  26.      */
  27.     public void setName(final String name) {
  28.         this.name = name;
  29.     }
  30.  
  31.     /**
  32.      * Getter for property "deceased"
  33.      * Different syntax for a boolean field (is vs. get)
  34.      */
  35.     public boolean isDeceased() {
  36.         return this.deceased;
  37.     }
  38.  
  39.     /**
  40.      * Setter for property deceased.
  41.      * @param deceased
  42.      */
  43.     public void setDeceased(final boolean deceased) {
  44.         this.deceased = deceased;
  45.     }
  46. }


TestPersonBean.java:

  1. import beans.PersonBean;
  2.  
  3. /**
  4.  * Class TestPersonBean.
  5.  */
  6. public class TestPersonBean {
  7.     /**
  8.      * Tester method main for class PersonBean.
  9.      * @param args
  10.      */
  11.     public static void main(String[] args) {
  12.         PersonBean person = new PersonBean();
  13.         person.setName("Bob");
  14.         person.setDeceased(false);
  15.  
  16.         // Output: "Bob [alive]"
  17.         System.out.print(person.getName());
  18.         System.out.println(person.isDeceased() ? " [deceased]" : " [alive]");
  19.     }
  20. }

testPersonBean.jsp;

  1. <% // Use of PersonBean in a JSP. %>
  2. <jsp:useBean id="person" class="beans.PersonBean" scope="page"/>
  3. <jsp:setProperty name="person" property="*"/>
  4.  
  5. <html>
  6. <body>
  7. Name: <jsp:getProperty name="person" property="name"/><br/>
  8. Deceased? <jsp:getProperty name="person" property="deceased"/><br/>
  9. <br/>
  10. <form name="beanTest" method="POST" action="testPersonBean.jsp">
  11. Enter a name: <input type="text" name="name" size="50"><br/>
  12. Choose an option:
  13. <select name="deceased">
  14.  <option value="false">Alive</option>
  15.  <option value="true">Dead</option>
  16. </select>
  17. <input type="submit" value="Test the Bean">
  18. </form>
  19. </body>
  20. </html>

阅读(1064) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~